我想序列化和反序列化一个实体及其依赖项,但我无法序列化涉及抽象类的元素。
等级制度 :
测试 --> 几个Calls
,其中Call
类是一个抽象类并由TestCallExecuteQuery
(与 相同的问题$conditions
)扩展
测试.php:
/**
* @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
* @ORM\Table(name="cfa_test")
* @JMSSer\ExclusionPolicy("all")
*/
class Test
{
/**
* @ORM\OneToMany(targetEntity="TestCall", mappedBy="test", cascade={"all"}, orphanRemoval=true)
* @JMSSer\Expose
* @JMSSer\Groups({"export"})
* @JMSSer\Type("ArrayCollection<App\Bundle\CapFileAnalyzerBundle\Entity\TestCall>")
*/
protected $calls;
/**
* @ORM\OneToMany(targetEntity="TestCondition", mappedBy="test", cascade={"all"}, orphanRemoval=true)
* @JMSSer\Expose
* @JMSSer\Groups({"export"})
* @JMSSer\Type("ArrayCollection<App\Bundle\CapFileAnalyzerBundle\Entity\TestCondition>")
*/
protected $conditions;
测试调用.php:
/**
* @ORM\Entity
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\Table(name="cfa_test_call")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({
* "executeQuery" = "App\Bundle\CapFileAnalyzerBundle\Entity\TestCallExecuteQuery",
* "call" = "App\Bundle\CapFileAnalyzerBundle\Entity\TestCall"
* })
* @JMSSer\ExclusionPolicy("all")
* @JMSSer\Discriminator(field="serializedType", map={
* "executeQuery"="App\Bundle\CapFileAnalyzerBundle\Entity\TestCallExecuteQuery",
* "call" = "App\Bundle\CapFileAnalyzerBundle\Entity\TestCall"
* })
*/
abstract class TestCall
{
/**
* @JMSSer\Expose
* @JMSSer\Groups({"export"})
*/
protected $type = 'call';
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Test", inversedBy="calls")
*/
protected $test;
/**
* @JMSSer\VirtualProperty()
* @JMSSer\SerializedName("serializedType")
*/
public function getDiscr()
{
return $this->type;
}
TestCallExecuteQuery.php :
/**
* @ORM\Entity
* @JMSSer\ExclusionPolicy("all")
*/
class TestCallExecuteQuery extends TestCall
{
protected $type = 'executeQuery';
/**
* @ORM\Column(name="`query`", type="text")
* @JMSSer\Expose
* @JMSSer\Groups({"export"})
*/
protected $query;
/**
* @ORM\Column(name="`return`", type="string", nullable=true)
* @JMSSer\Expose
* @JMSSer\Groups({"export"})
*/
protected $return;
所以我按照互联网上的说明进行操作:
@JMSSer\Expose
@JMSSer\ExclusionPolicy("all")
在每个班级中都带有注释@JMSSer\Discriminator
抽象类顶部的注释TestCall
以与扩展类 (TestcallExecuteQuery
)映射
但是..当我序列化时,我只得到 TestCall 的类型属性,而不是query
或return
定义在中的属性TestCallExecuteQuery
:
{"tests":[{"calls":[{"type":"executeQuery"},{"type":"executeQuery"}], ... }
我知道这是可能的,因为我曾经得到过它们,但即使时光倒流我也无法重现。
{"tests":[{"calls":[{"query":"SELECT * FROM table","return":"return_1"}], ... }
编辑 :
好的,我可能得到query
并return
通过改变Test.php
:
/**
* @JMSSer\Type("ArrayCollection<App\Bundle\CapFileAnalyzerBundle\Entity\TestCall>")
*/
protected $calls;
至 :
/**
* @JMSSer\Type("ArrayCollection<App\Bundle\CapFileAnalyzerBundle\Entity\TestCallExecuteQuery>")
*/
protected $calls;
我究竟做错了什么 ?