我的项目中有这两个实体
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();
}
}
这是我的另一个实体
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;
}
我试图在我的控制器中运行此代码
$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) );
但我得到了这个例外
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
我根据学说注释映射了我的实体。我错过了什么吗?
使用useCircularReferenceLimit
方法。例如:
$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);
原因是当您尝试序列化它们时,实体中的循环引用会导致一些问题。该方法的作用是定义序列化层次结构的最大深度。
编辑:添加循环引用处理程序(A circular reference has been detected (configured limit: 1) Serializer SYMFONY)
要试用Symfony 3.2,但circular_reference_limit
不是问题(默认为1是好的,否则你的实体将被检索2次),问题是circular_reference_handler
处理实体的方式。告诉id
是实体标识符可以解决问题。请参阅Symfony Docs at the bottom of this paragraph。
由于setCircularReferenceHandler
被弃用in favour of the following keys of the context circular_reference_handler
,我们可以写道:
// 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']);
修复了同样的问题
use JMS\Serializer\SerializerBuilder;
...
$products = $em->getRepository('AppBundle:Product')->findAll();
$serializer = SerializerBuilder::create()->build();
$jsonObject = $serializer->serialize($products, 'json');
阅读here