2010-11-11 19 views
1

我遇到了Doctrine ODM的問題。當我創建一個簡單的EmbedMany關係到另一個文件(即具有許多意見嵌入文檔的nickpage)的實體,我嘗試清除,那麼這個集合仍然與所有元素存在:Doctrine ODM - 清除/更新/刪除EmbedDocument關係

/** @Document */ 
class Nickpage 
{ 
    ... 

    /** @EmbedMany(targetDocument="Comment") */ 
    protected $comments = array(); 

    ... 

    public function clearComments() 
    { 
     $this->comments = array(); 
     // or: 
     // $this->comments = new \Doctrine\Common\Collections\ArrayCollection(); 
     return $this; 
    } 
} 

當我只需加載相關評論的暱稱即可,我致電

$nickpage->clearComments(); 
$dm->persist($nickpage); 
$dm->flush(); 

評論依然存在。同樣的現象發生時,我刪除ArrayCollection $ comments中的一些元素和我沖洗...

我是在處理這種關係(即使用ReferenceMany)的錯誤方式或是它是錯誤的方式來處理引用?

格爾茨來自德國, 安迪

回答

2

你不必叫堅持,因爲你的頁面已經被原則進行管理。您只需調用flush即可推送數據庫中的更新。

而且您不需要添加clearComments方法。您可以使用ArrayCollection的清除方法:

$nickpage->comments->clear();