Symfony 3.2 Circular reference detected (configured limit: 1)

I have two objects in my project

class PoliceGroupe { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="code", type="string", length=50) */ private $code; /** * @ORM\ManyToMany(targetEntity="PointVente", inversedBy="policegroupe") * @ORM\JoinTable(name="police_groupe_point_vente", * joinColumns={@ORM\JoinColumn(name="police_groupe_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="point_vente_id", referencedColumnName="id")} * ) */ private $pointVente; /** * Constructor */ public function __construct($produit) { $this->pointVente = new \Doctrine\Common\Collections\ArrayCollection(); } } 

And here is my other essence

 class PointVente { /** * @var string * * @ORM\Column(name="abb", type="string", length=50) */ private $abb; /** * @var string * * @ORM\Column(name="libelle", type="string", length=255) */ private $libelle; /** * * @ORM\ManyToMany(targetEntity="PoliceGroupe", mappedBy="pointVente") */ private $policegroupe; } 

and I'm trying to run this code in my controller

 $encoders = array(new XmlEncoder(), new JsonEncoder()); $normalizers = array(new ObjectNormalizer()); $serializer = new Serializer($normalizers, $encoders); $em = $this->getDoctrine()->getManager(); $data = $request->get('data'); $policegroupe=$em->getRepository('StatBundle:PoliceGroupe')->findOneBy(array('id' => $data)); $pointventes = $policegroupe->getPointVente(); $jsonContent = $serializer->serialize($pointventes, 'json'); return new JsonResponse( array('pointventes'=>$jsonContent) ); 

But I get this exception

 Symfony\Component\Serializer\Exception\CircularReferenceException: A circular reference has been detected (configured limit: 1). at n/a in C:\wamp\www\Sys\vendor\symfony\symfony\src\Symfony\Component\Serializer\Normalizer\AbstractNormalizer.php line 194 

I mapped my entities in accordance with the annotations of the doctrine. Did I miss something?

+12
source share
3 answers

Symfony 3.2

Use the useCircularReferenceLimit method. For instance:

 $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 circular references in your entities cause some problems when trying to serialize them. The result of the method is to determine the maximum depth of the serialization hierarchy.

Change: added loopback handler ( Loopback detected (restriction configured: 1) SYMFONY serializer )

UPDATE: update (Symfony 4.2)

Test with Symfony 3.2, but here circular_reference_limit not a problem (by default 1 is OK, otherwise your entity will be extracted 2 times), the problem is how it is processed by circular_reference_handler . A message stating that id is an entity identifier solves the problem. See the Symfony docs at the bottom of this paragraph .

Since setCircularReferenceHandler is deprecated in favor of the following circular_reference_handler context keys , 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']); 
+27
source

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

But we have another option. Alternatively, there is a way to ignore attributes from the source object. We can ignore this if we definitely do not 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 these attributes, use the setIgnoredAttributes () method in 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 during serialization. Starting with Symfony 2.7, they are also ignored during deserialization.

Symfony 4.2 - 5.0

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

To remove these attributes, provide the array using 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 combination of the two methods setIgnoredAttributes() and setCircularReferenceLimit() , and it works fine.

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

0
source

Fixed same issue with

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

Read here

-1
source

All Articles