FOSRestBundle的序列化程序使用继承的实体抛出递归错误(FOSRestBundle's serializer throws recursion error using inherited entities)

我正在开发一个继承抽象类的应用程序。 这些抽象类具有自己的序列化器映射,如下面的示例所示

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);

I’m developing an application that inherits abstract classes. These abstract classes have their own mapping for the serializer as shown in the example bellow

Hezten\CoreBundle\Model\Enroled: exclusion_policy: ALL

And the abstract class:

<?php namespace Hezten\CoreBundle\Model; abstract class Enroled implements EnroledInterface { protected $student; protected $subject; //Some functions... }

The class that inherits the previous class looks as follows

<?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; }

The error thrown says this:

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

For sure, I'm doing something wrong as no entities are exposed, just three fields of "Enroled" entity according to the mappings but I have no clue. I spent a couple of days trying to figure out what's the mistake without success.

What is the proper way to do the mapping of inhertied properties?

Update

Code used to serialize JSON using FOSRestBundle:

$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);

最满意答案

最后API正在运行......我必须覆盖我继承的类的元数据,将以下行添加到config.yml

jms_serializer: metadata: directories: HeztenCoreBundle: namespace_prefix: "Hezten\\CoreBundle" path: "%kernel.root_dir%/serializer/HeztenCoreBundle"

在上面选择的路径中,我为每个模型设置排除策略添加了一个yml文件到ALL:

Hezten\CoreBundle\Model\Enroled: exclusion_policy: ALL

我在继承这些模型的实体上使用注释来公开所需的信息。

我不知道这是否是最好的方法,但对我来说效果很好

Finally the API is working... I had to override the metadata of the classes I was inheriting adding the following lines to the config.yml

jms_serializer: metadata: directories: HeztenCoreBundle: namespace_prefix: "Hezten\\CoreBundle" path: "%kernel.root_dir%/serializer/HeztenCoreBundle"

In the path that is selected above I added one yml file for each Model setting exclusion policy to ALL:

Hezten\CoreBundle\Model\Enroled: exclusion_policy: ALL

And I used annotations on the entities that were inheriting those models to expose required info.

I don't know if this is the best approach but works well for me

更多推荐