Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm trying to serialize embedded mongodb documents with JMSSerizial Bundle

I'm try to serialize a MongoDB document with embedded documents within Symfony 2.1. I am using the JMSserializer and Mongodb-odm bundles.

I have the following Documents entities.

// Blog

namespace App\DocumentBundle\Document;

use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use JMS\SerializerBundle\Annotation\Type;

/**
 * @MongoDB\Document(repositoryClass="App\DocumentBundle\Repository\BlogRepository")
 */
class Blog {

    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     * @Assert\NotBlank()
     */
    protected $title;

    /**
     * @MongoDB\string
     * @Assert\NotBlank()
     */
    protected $blog;

    /**
     * @MongoDB\EmbedMany(targetDocument="Tag")
     */
    private $tags;

    /**
     * @MongoDB\Timestamp
     */
    protected $created;

    /**
     * @MongoDB\Timestamp
     */
    protected $updated;
}

and

// Tag

namespace App\DocumentBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\EmbeddedDocument
 */
class Tag {

    /**
     * @MongoDB\String
     */
    protected $name;
}

An ArrayCollection type is generated for the tag attribute, but the JMSSerializer bundle doesn't like it. If I change the tag to @MongoDB\String and regenerate the Blog document, then serialization occurs, but not with @MongoDB\EmbedMany(targetDocument="Tag") set.

Do I need to specify some of the JMSSerializer annotated attributes allow embedded document to also be serialized?

like image 711
Philip Avatar asked Nov 08 '12 07:11

Philip


1 Answers

You have to configure the expected type for JMSSerializer

Annotation :

/**
 * @MongoDB\EmbedMany(targetDocument="Tag")
 * @Type(ArrayCollection<App\DocumentBundle\Document\Tag>)
 */
private $tags;

Yaml :

tags:
    expose: true
    type: ArrayCollection<App\DocumentBundle\Document\Tag>
like image 177
maphe Avatar answered Sep 28 '22 06:09

maphe