Arrays 条令ODM-清除/更新/删除文档关系
我遇到了ODM的问题。当我创建一个与另一个文档具有简单EmbedMany关系的实体(即,一个带有许多注释的nickpage作为嵌入文档)并尝试清除集合时,集合仍然存在,其中包含所有元素:Arrays 条令ODM-清除/更新/删除文档关系,arrays,collections,doctrine,mongodb,reference,Arrays,Collections,Doctrine,Mongodb,Reference,我遇到了ODM的问题。当我创建一个与另一个文档具有简单EmbedMany关系的实体(即,一个带有许多注释的nickpage作为嵌入文档)并尝试清除集合时,集合仍然存在,其中包含所有元素: /** @Document */ class Nickpage { ... /** @EmbedMany(targetDocument="Comment") */ protected $comments = array(); ... public function clear
/** @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时,我调用
$nickpage->clearComments();
$dm->persist($nickpage);
$dm->flush();
评论仍然存在。当我删除ArrayCollection$comments中的某些元素并刷新时,也会出现相同的现象
我处理此类关系(即使用ReferenceMany)的方式是否错误,或者处理这些关系的方式是否错误
来自德国的格里茨,
Andi您不必调用persist,因为您的页面已经由Doctrine管理。您只需调用flush就可以在数据库中推送更新 您不需要添加clearComments方法。您可以使用ArrayCollection的clear方法:
$nickpage->comments->clear();