JMS Serializers没有序列化集合

时间:2014-08-13 12:06:14

标签: php symfony doctrine-orm jms

与其他问题相关:Serialize Doctrine array containing objects using inheritance

我开始简化相同的代码,只使用Test的集合序列化一个类Ds

我将做什么,我将序列化字符串作为{"name":"Test name","collection":[[],[]]}

代码:

        echo 'Test entities<br>';

        $test = new Test();
        $test->setName('Test name');


        $d1 = new D();
        $d1->setDescription('adfasfd');

        $d2 = new D();
        $d2->setDescription('adfasfd');


        $test->addCollection($d1);
        $test->addCollection($d2);

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

测试

<?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\Column(type="string", length=100)
     */
    protected $name;

    /**
     * #@ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\B", mappedBy="test", cascade={"all"})
     * #@JMS\Type("ArrayCollection<Acme\DemoBundle\Entity\B>")
     * @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\D", mappedBy="test", cascade={"all"})
     */
    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\D $collection) {
        $this->collection[] = $collection;

        return $this;
    }

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

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

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

    public function getName() {
        return $this->name;
    }

}

d

<?php

namespace Acme\DemoBundle\Entity;

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

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

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

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

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

}

0 个答案:

没有答案