问题描述
自从升级到 Symfony 2.7 后,在尝试序列化与给定组关联的联系人数组时,我似乎不断收到检测到循环引用"错误.它们建立在多对多关联中(一个组有多个联系人;一个联系人有多个组关联).
Since upgrading to Symfony 2.7, I seem to keep getting 'circular reference has been detected' errors when attempting to serialize an array of contacts associated with a given group. They're setup in a many-to-many association (one group has many contacts; one contact has many group-associations).
现在,我按照 2.7 升级帖子,但似乎仍然出现错误.我的控制器代码目前如下:
Now, I followed the guide for using serialization groups as per the 2.7 upgrade post, but still seem to get the error. My controller code for this is currently as follows:
$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')
));
在运行 $serializer->serialize()
时,我在 1 次循环引用后得到 CircularReferenceException.到目前为止,我已经像这样配置了我的 Contact 实体,并带有 @Groups 注释:
When running $serializer->serialize()
, I get the CircularReferenceException after 1 circular reference. So far I have my Contact entity configured like so, with the @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
}
还有我的 ContactGroup 实体:
And my ContactGroup entity:
/**
* 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 circularity problem? Many thanks.
推荐答案
看起来配置有问题.您必须启用序列化组注释:
It looks like something wrong with config.You have to enable serialization groups annotation:
# app/config/config.yml
framework:
# ...
serializer:
enable_annotations: true
ContactGroup 实体类中必须存在正确的 use 语句
And proper use statement has to be present in ContactGroup entity class
use Symfony\Component\Serializer\Annotation\Groups;
这篇关于序列化多对多关联对象时出现“检测到循环引用"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!