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.