1
我正在开发一个继承抽象类的应用程序。这些抽象类具有串行自己的地图所示的例子娄FOSRestBundle的序列化器使用继承实体抛出递归错误
Hezten\CoreBundle\Model\Enroled:
exclusion_policy: ALL
与抽象类:
<?php
namespace Hezten\CoreBundle\Model;
abstract class Enroled implements EnroledInterface
{
protected $student;
protected $subject;
//Some functions...
}
继承以前的类看起来如下
<?php
namespace XXX\XXXBundle\Entity;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Exclude;
use Doctrine\ORM\Mapping as ORM;
use Hezten\CoreBundle\Model\Enroled as BaseEnroled;
/**
* @ORM\Entity
* @ExclusionPolicy("NONE")
*/
class Enroled extends BaseEnroled
{
/** @ORM\Id
* @ORM\ManyToOne(targetEntity="XXX\XXXBundle\Entity\Student", inversedBy="enroled")
* @Exclude
*/
protected $student;
/** @ORM\Id
* @ORM\ManyToOne(targetEntity="XXX\XXXBundle\Entity\Subject", inversedBy="enroled")
* @Exclude
*/
protected $subject;
/** @ORM\Column(type="boolean") */
private $field0;
/** @ORM\Column(type="boolean")
*/
private $field1;
/** @ORM\Column(type="boolean") */
private $field2;
}
类
抛出的错误说这个:
Warning: json_encode() [<a href='function.json-encode'>function.json-encode</a>]: recursion detected in C:\xampp\htdocs\Project\vendor\jms\serializer\src\JMS\Serializer\JsonSerializationVisitor.php line 29
当然,我做错了,因为没有实体暴露,根据映射只有三个“Enroled”实体字段,但我不知道。我花了几天的时间试图找出没有成功的错误。
什么是正确的方法做映射的被盗属性?
更新
代码用于使用FOSRestBundle序列化JSON:
$students = $this->get('hezten_core.manager.enroled')->findEnroledBySubject($subject);
$view = View::create()
->setStatusCode(200)
->setFormat('json')
->setData($students);
return $this->get('fos_rest.view_handler')->handle($view);
看一看http://stackoverflow.com/questions/6706485/how-to-encode-doctrine-entities-to -json-in-symfony-2-0-ajax-application ...和... http://stackoverflow.com/questions/11851197/avoiding-recursion-with-doctrine-entities-and-jmsserializer ... Can您提供了用于序列化为JSON的代码? – nifr
尝试将'@ ExclusionPolicy'设置为'“none”'而不是'“NONE”'来匹配文档值......但我不认为就是这样。 http://jmsyst.com/libs/serializer/master/reference/annotations#exclusionpolicy – nifr
您的序列化JSON输出究竟应该如何查看? – nifr