使用继承序列化包含对象的Doctrine数组

Roe*_*zen 26 php symfony doctrine-orm jmsserializerbundle

问题:

序列化Doctrine enitities集合时,虽然项目为空,但集合仍然有2个项目.

背景:

我有一些实体相互B延伸延伸AC延伸B.在实体中,Test我有一个包含该类型对象的数组B.$test将在序列化时具有预期值(具有两个项目的集合).

$test包含一个变量(数组)数组中的collection一个项是类型B和一个类型C.

$sTest虽然项目是空的,但会获得两个项目的集合.这是$sTest串行化后字符串的外观$test "{"collection":[[],[]]}"

测试脚本:

$test = new Test();

$b = new B();
$b->setToken('asdf');
$b->setName('asdf');

$c = new C();
$c->setToken('asdf');
$c->setName('asdf');
$c->setDescription('asdf');

$test->addCollection($b);
$test->addCollection($c);

//Serialize
$serializer = $this->container->get('serializer');
$sTest = $serializer->serialize($test, 'json');

//Deserialize
$deserializer = $this->container->get('serializer');
$dTest = $deserializer->deserialize($sTest, 'Acme\DemoBundle\Entity\Test', 'json');

$em = $this->getDoctrine()->getManager();

$em->merge($dTest);
$em->flush();
Run Code Online (Sandbox Code Playgroud)

A:

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"a" = "Acme\DemoBundle\Entity\A", "b" = "Acme\DemoBundle\Entity\B", "c" = "Acme\DemoBundle\Entity\C"})
 * 
 * @JMS\ExclusionPolicy("None")
 * @JMS\Discriminator(field = "type", map = {
 *          "a": "Acme\DemoBundle\Entity\A",
 *          "b": "Acme\DemoBundle\Entity\B",
 *          "c": "Acme\DemoBundle\Entity\C", * 
 *  })
 */
class A {

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $token;

    public function setToken($token){
        $this->token = $token;
    }    

    /**
     * @JMS\VirtualProperty
     * @JMS\SerializedName("type")
     */
    public function getDiscr()
    {
        return 'a';
    }

}
Run Code Online (Sandbox Code Playgroud)

B:

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * @ORM\Entity
 * @JMS\ExclusionPolicy("None")
 */
class B extends A {

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;

    /**
     * @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Test", inversedBy="collection")
     * @ORM\JoinColumn(name="TestId", referencedColumnName="id")
     */
    private $test;

    public function setName($name) {
        $this->name = $name;
    }

    /**
     * @JMS\VirtualProperty
     * @JMS\SerializedName("type")
     */
    public function getDiscr() {
        return 'b';
    }


    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set token
     *
     * @param string $token
     * @return B
     */
    public function setToken($token)
    {
        $this->token = $token;

        return $this;
    }

    /**
     * Get token
     *
     * @return string 
     */
    public function getToken()
    {
        return $this->token;
    }

    /**
     * Set test
     *
     * @param \Acme\DemoBundle\Entity\Test $test
     * @return B
     */
    public function setTest(\Acme\DemoBundle\Entity\Test $test = null)
    {
        $this->test = $test;

        return $this;
    }

    /**
     * Get test
     *
     * @return \Acme\DemoBundle\Entity\Test 
     */
    public function getTest()
    {
        return $this->test;
    }
}
Run Code Online (Sandbox Code Playgroud)

C:

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * @ORM\Entity
 * @JMS\ExclusionPolicy("None")
 */
class C extends B {

    /**
     * @ORM\Column(type="text")
     */
    protected $description;

    public function setDescription($description) {
        $this->description = $description;
    }

    /**
     * @JMS\VirtualProperty
     * @JMS\SerializedName("type")
     */
    public function getDiscr() {
        return 'c';
    }

}
Run Code Online (Sandbox Code Playgroud)

测试:

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;

/**
 * @ORM\Entity
 */
class Test {

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\B", mappedBy="test", cascade={"all"})
     * @JMS\Type("ArrayCollection<'Acme\DemoBundle\Entity\B'>")
     */
    private $collection;


    /**
     * Constructor
     */
    public function __construct()
    {
        $this->collection = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add collection
     *
     * @param \Acme\DemoBundle\Entity\B $collection
     * @return Test
     */
    public function addCollection(\Acme\DemoBundle\Entity\B $collection)
    {
        $this->collection[] = $collection;

        return $this;
    }

    /**
     * Remove collection
     *
     * @param \Acme\DemoBundle\Entity\B $collection
     */
    public function removeCollection(\Acme\DemoBundle\Entity\B $collection)
    {
        $this->collection->removeElement($collection);
    }

    /**
     * Get collection
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getCollection()
    {
        return $this->collection;
    }
}
Run Code Online (Sandbox Code Playgroud)

Noy*_*Noy 0

对于稍微不同的要求(JSON),我创建了一个基本实体并实现了如下行为:

function jsonSerialize()
{
    $arr = get_object_vars($this);
    foreach ($arr as $key => $var) {
        unset($arr[$key]);
        if (method_exists($var, 'jsonSerialize')) {
            $var = $var->jsonSerialize();
        } elseif ( $var instanceof \DateTime) {
            $var = $var->format('Y-m-d\TH:i:s\Z');
        }
        $arr[strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $key))] = $var;
    }
    return $arr;
}
Run Code Online (Sandbox Code Playgroud)