"Circular link was detected" error when serializing objects associated with many to many

Starting from the upgrade to Symfony 2.7, it seems that when I try to serialize an array of contacts associated with this group, it seems to me that when the error is detected, "a circular link was detected." They are configured in many-to-many associations (one group has many contacts, one contact has many group associations).

Now I turned to the guide on using serialization groups according to the update message , but still it seems I got an error. My controller code for this currently looks like this:

$group = $this->getDoctrine() ->getRepository('TwbGroupsBundle:ContactGroup') ->find($id); $groupContacts = $group->getContacts(); $encoder = new JsonEncoder(); $normalizer = new ObjectNormalizer(); $serializer = new Serializer(array($normalizer), array($encoder)); $json = $serializer->serialize($groupContacts, 'json', array( 'groups' => array('public') )); 

When I run $serializer->serialize() I get a CircularReferenceException after 1 round link. So far, I have a Contact object configured like this with @Groups annotations:

 /** * Contact * * @ORM\Table(name="tblContacts") * @ORM\Entity(repositoryClass="Twb\Bundle\ContactsBundle\Entity\Repository\ContactRepository") */ class Contact implements ContactInterface { /** * @var string * * @ORM\Column(name="ContactName", type="string", length=50, nullable=true) * @Groups({"public"}) */ private $contactname; /** * @var integer * * @ORM\Column(name="ContactID", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") * @Groups({"public"}) */ private $contactid; /** * * @var ArrayCollection * @ORM\ManyToMany(targetEntity="Twb\Bundle\GroupsBundle\Entity\ContactGroup", inversedBy="contacts") * @ORM\JoinTable(name="tblContactsGroupsAssignments", * joinColumns={@ORM\JoinColumn(name="contactId", referencedColumnName="ContactID")}, * inverseJoinColumns={@ORM\JoinColumn(name="contactGroupId", referencedColumnName="id")} * ) */ protected $contactGroups; // ...getters/setters and so on } 

And my ContactGroup object:

 /** * ContactGroup * * @ORM\Table(name="tblContactsGroups") * @ORM\Entity */ class ContactGroup { // ... /** * * @var Contact * * @ORM\ManyToMany(targetEntity="Twb\Bundle\ContactsBundle\Entity\Contact", mappedBy="contactGroups") */ private $contacts; // ... } 

Is there something I'm missing here to get around the roundness problem? Thank you very much.

+5
source share
2 answers
  $normalizers->setCircularReferenceHandler(function ($object) { return $object->getId(); }); 

Just add it after instantiating your Normalizer object;

+1
source

There seems to be something wrong with config. You must enable annotation of serialization groups:

 # app/config/config.yml framework: # ... serializer: enable_annotations: true 

And the correct use statement must be present in the ContactGroup entity class

 use Symfony\Component\Serializer\Annotation\Groups; 
0
source

Source: https://habr.com/ru/post/1233557/


All Articles