【问题标题】:Doctrine ODM - Clear / update / remove EmbedDocument relationsDoctrine ODM - 清除/更新/删除 EmbedDocument 关系
【发布时间】:2010-11-11 12:54:21
【问题描述】:

我遇到了 Doctrine ODM 的问题。当我创建一个与另一个文档具有简单 EmbedMany 关系的实体(即,一个包含许多 cmets 作为嵌入文档的昵称),并尝试清除集合时,该集合仍然存在,包含所有元素:

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

当我简单地加载带有相关 cmets 的昵称时,我会调用

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

cmets 仍然存在。当我删除 ArrayCollection $cmets 中的一些元素并刷新时,也会出现同样的现象...

我是在处理此类关系时采用了错误的方式(即使用 ReferenceMany)还是使用引用的方式有误?

来自德国的 Greetz, 安迪

【问题讨论】:

    标签: arrays collections doctrine mongodb reference


    【解决方案1】:

    您不必调用persist,因为您的页面已经由Doctrine 管理。您只需调用flush即可将更新推送到数据库中。

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

    $nickpage->comments->clear();
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 2012-07-03
      • 2022-11-24
      • 2017-02-27
      • 1970-01-01
      • 2021-04-16
      • 2015-01-09
      • 2020-02-16
      • 2016-10-14
      相关资源
      最近更新 更多