Symfony 3.2 A circular reference has been detected (configured limit: 1)

百般思念 提交于 2019-11-30 08:43:33
Ilario Pierbattista

Symfony 3.2

Use the useCircularReferenceLimit method. For example:

$normalizer = new ObjectNormalizer();
$normalizer->setCircularReferenceLimit(2);
// Add Circular reference handler
$normalizer->setCircularReferenceHandler(function ($object) {
    return $object->getId();
});
$normalizers = array($normalizer);
$serializer = new Serializer($normalizers, $encoders);

The reason is that the circular referencing in your entities causes some problems when you try to serialize them. The effect of the method is to define the maximum depth of the serialization hierarchy.

Edit: Added circular reference handler (A circular reference has been detected (configured limit: 1) Serializer SYMFONY)

EDIT : Update (Symfony 4.2)

To be tried with Symfony 3.2, but the circular_reference_limit is not the problem here (and the defaults to 1 is OK, else your entity will be retrieved 2 times), the problem is the way the entity is handled by circular_reference_handler. Telling that id is the entity identifier solves the problem. See Symfony Docs at the bottom of this paragraph.

Since setCircularReferenceHandler is deprecated in favour of the following keys of the context circular_reference_handler, we can write:

// Tip : Inject SerializerInterface $serializer in the controller method
// and avoid these 3 lines of instanciation/configuration
$encoders = [new JsonEncoder()]; // If no need for XmlEncoder
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);

// Serialize your object in Json
$jsonObject = $serializer->serialize($objectToSerialize, 'json', [
    'circular_reference_handler' => function ($object) {
        return $object->getId();
    }
]);

// For instance, return a Response with encoded Json
return new Response($jsonObject, 200, ['Content-Type' => 'application/json']);

The best way is to use the useCircularReferenceLimit method. As it has already been clearly explained in this post.

But we have another option. As an option, there's a way to ignore attributes from the origin object. We can ignore it if we definitely don't need it in a serialized object. The advantage of this solution is that the serialized object is smaller and easier to read, and the disadvantage is that we will no longer refer to the ignored attribute.

Symfony 2.3 - 4.1

To remove those attributes use the setIgnoredAttributes() method on the normalizer definition:

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

$normalizer = new ObjectNormalizer();
$normalizer->setIgnoredAttributes(array('age'));
$encoder = new JsonEncoder();

$serializer = new Serializer(array($normalizer), array($encoder));
$serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsperson":false}

The setIgnoredAttributes() method was introduced in Symfony 2.3.

Prior to Symfony 2.7, attributes were only ignored while serializing. Since Symfony 2.7, they are ignored when deserializing too.

Symfony 4.2 - 5.0

The setIgnoredAttributes() method that was used as an alternative to the ignored_attributes option was deprecated in Symfony 4.2.

To remove those attributes provide an array via the ignored_attributes key in the context parameter of the desired serializer method:

use Acme\Person;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$person = new Person();
$person->setName('foo');
$person->setAge(99);

$normalizer = new ObjectNormalizer();
$encoder = new JsonEncoder();

$serializer = new Serializer([$normalizer], [$encoder]);
$serializer->serialize($person, 'json', ['ignored_attributes' => ['age']]); // Output: {"name":"foo"}

In my Symfony 3.4 projects I use a mix of these two methods setIgnoredAttributes() and setCircularReferenceLimit() and it works fine.

Source: https://symfony.com/doc/3.4/components/serializer.html

Fixed same issue by

use JMS\Serializer\SerializerBuilder;
...

$products = $em->getRepository('AppBundle:Product')->findAll();
$serializer = SerializerBuilder::create()->build();
$jsonObject = $serializer->serialize($products, 'json');

Read here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!