0
votes

I have been using the "jms/serializer": "0.13.*@dev" in order to serialize my objects.

I am using it in a Zend Framework (2) and a Doctrine project.

This is my code:

use JMS\Serializer\SerializerBuilder as SerializerBuilder;
(....)

public function getList() {

    $em = $this->getEntityManager();
    $repo = $em->getRepository('MyApp\Entity\Product');
    $hydrator = new DoctrineHydrator($em);

    $data = array();
    foreach ($repo->findAll() as $found) {
        $data[] = $hydrator->extract($found);
    }
    $serializer = SerializerBuilder::create()->build();
    $jsonContent = $serializer->serialize($data, 'json');

    $this->getResponseWithHeader()->setStatusCode(self::OK_200);


    return new JsonModel($jsonContent);
}

But I am getting this error:

Resources are not supported in serialized data. Path: MyApp\Entity\FOO -> Doctrine\ORM\PersistentCollection -> MyApp\Entity\Product -> Doctrine\ORM\PersistentCollection -> DoctrineORMModule\Proxy__CG__\MyApp\MyApp\Brand

Apparently you can't serialize persistent collections.

I have googled around and found this Symfony related question. But how can I solve this problem within the stand alone Serializer library?

Thanks very much.


EDIT

Can this have anything to do with JMS annotations? Should I use certain annotations to get this working?

1

1 Answers

1
votes

I'm guessing the issue here is that the PersistentCollection may contain a reference to a database connection so JMS can't handle the Resource type.

If you only want the first level serialised then I guess you could hack at it like:

foreach ($repo->findAll() as $found) {
    // Set these to basically empty arrays
    $found->setRelation1(new \Doctrine\Common\Collections\ArrayCollection);
    $found->setRelation2(new \Doctrine\Common\Collections\ArrayCollection);
    $data[] = $hydrator->extract($found);
}

Or you could extend the JMS Serialize function to just ignore Resource collections rather than throwing an Exception.