2012-11-08 13 views
6

我試圖用Symfony 2.1內嵌文檔序列化一個MongoDB文檔。我正在使用JMSserializer和Mongodb-odm軟件包。我試圖用JMSSerizial Bundle序列化嵌入式MongoDB文檔

我有以下文檔實體。

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

// Tag 

namespace App\DocumentBundle\Document; 

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

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

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

一個ArrayCollection類型的標籤屬性生成,但JMSSerializer束不喜歡它。如果我將標籤更改爲@MongoDB \ String並重新生成博客文檔, 將發生序列化,但不會與@MongoDB \ EmbedMany(targetDocument =「Tag」)設置。

我是否需要指定某些JMSSerializer註釋屬性允許嵌入式文檔也被序列化?

+0

您的代碼似乎罰款。您是否使用捆綁包的最新版本?此外,您認爲JMSSerializer不喜歡它是什麼意思?什麼是拋出的錯誤信息? – kratos

+1

爲什麼不用''@MongoDB \ Collection''註釋''tags'',如果你只是存儲標籤名稱而已?嘗試使用註解http://jmsyst.com/libs/將'ArrayCollection'指定爲類型'''tag1','tag2','tag3','etc'] –

+0

串行器/主/參考/註釋#類型 –

回答

0

您必須配置的預期類型JMSSerializer

譯註:

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

YAML:

tags: 
    expose: true 
    type: ArrayCollection<App\DocumentBundle\Document\Tag>