2011-11-27 46 views
1

如何使用Doctrine ODM將文檔存儲在另一個文檔中?如何使用Doctrine ODM將文檔存儲在另一個文檔中?

我在文檔中看不到數組或Json類型。

我希望能夠做這樣的事情:

class Post { 

    /** 
    * @MongoDB\String 
    */ 
    protected $body; 

    /** 
    * @MongoDB\Array 
    */ 
    protected $comments = array(); 

} 

我不希望有徵求意見的獨立集合。我希望他們保存在每個帖子裏面。

回答

2
/** 
* @MongoDB\Document 
*/ 
class Post 
{ 
    /** 
    * @MongoDB\Id 
    */ 
    private $id; 

    /** 
    * @MongoDB\String 
    */ 
    private $body; 

    /** 
    * @MongoDB\EmbedMany(targetDocument="Comment") 
    */ 
    private $comments; 

    public function __construct() 
    { 
     $this->comments = new ArrayCollection(); 
    } 
} 

/** 
* @MongoDB\EmbeddedDocument 
*/ 
class Comment 
{ 
    /** 
    * @MongoDB\String 
    */ 
    private $body; 
} 

但請注意,評論,則不適合嵌入 - 相反,MongoDB中埋件的可能是最流行的例子。我從嵌入的意見開始,但後來遇到了一些問題,並決定將它們存儲在單獨的集合中。我不記得所有的問題,但主要的問題是無法對數據庫端的評論進行排序。快速解決方案是在客戶端對它們進行分類,但是當涉及到分頁時,它不能進行擴展。

+0

我曾問嘗試過這一點,但它說的ArrayCollection()無效或東西。列舉檢查 – HappyDeveloper

+0

Class Doctrine \ Common \ Collections \ ArrayCollection不是有效的文檔或映射的超類。 – HappyDeveloper

+0

您是否嘗試清除緩存? MongoDBBundle有一些緩存問題,所以即使在開發環境中,有時候也需要清除它。 –

-1

在我的__construct()我需要

new \Doctrine\Common\Collections\ArrayCollection(); 

,你只需要

new ArrayCollection(); 
相關問題