I want to serialize and deserialize an entity with its dependancies but I am unable to serialize elements that concern an abstract class.
Hierarchy :
Test --> several Calls
where Call
class is an abstract class and is extended by TestCallExecuteQuery
(same issue with $conditions
)
Test.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;
TestCall.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;
So I followed instructions found over Internet :
-
@JMSSer\Expose
annotation with@JMSSer\ExclusionPolicy("all")
in every class -
@JMSSer\Discriminator
annotation on top of abstract classTestCall
to map with extender class (TestcallExecuteQuery
)
But.. When I serialize I get only type property of TestCall but not query
or return
property defined in TestCallExecuteQuery
:
{"tests":[{"calls":[{"type":"executeQuery"},{"type":"executeQuery"}], ... }
I know it's possible because I got them ONCE but I was unable to reproduce that even by turning back the clock..
{"tests":[{"calls":[{"query":"SELECT * FROM table","return":"return_1"}], ... }
EDIT :
Ok I probably got query
and return
by changing in Test.php
:
/**
* @JMSSer\Type("ArrayCollection<App\Bundle\CapFileAnalyzerBundle\Entity\TestCall>")
*/
protected $calls;
To :
/**
* @JMSSer\Type("ArrayCollection<App\Bundle\CapFileAnalyzerBundle\Entity\TestCallExecuteQuery>")
*/
protected $calls;
What am I doing wrong ?