4

Related to an other problem: Serialize Doctrine array containing objects using inheritance

I started to simplefy the same code to only serialize a class Test with a collection of Ds.

What ever I wil do I get the serialized string as {"name":"Test name","collection":[[],[]]}.

Code:

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

Test:

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

}
Community
  • 1
  • 1
Roel Veldhuizen
  • 4,613
  • 8
  • 44
  • 78

0 Answers0