问题:Doctrine Mongo cascade remove children when child is the owning side

我有一个父/子 OneToMany 参考:

/**
 * @ODM\Document
 */
class Parent
{
    // ...

    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ODM\ReferenceMany(targetDocument="Child", mappedBy="parent")
     */
    protected $children;

    // ...
}



/**
 * @ODM\Document
 */
class Child
{

    // ...

    /**
     * @var Parent
     * @ODM\ReferenceOne(targetDocument="Parent", inversedBy="children", orphanRemoval=true)
     */
    protected $parent;

    // ...

}

我想要的是当父母被移除时,它的所有孩子也应该被移除。我在父注释上尝试了 cascadeu003d{"remove"} 和 orphanRemovalu003dtrue ,但它似乎不起作用。

我想知道是否有一个选项可以自动执行此操作,而无需编写 LifeCycleEventListener。

谢谢

解答

在父类中?

class Parent
{
    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ODM\ReferenceMany(targetDocument="Child", mappedBy="parent", cascade={"remove"})
     */
    protected $children;

    // ...
}

在我的项目中,cascade={'remove'}完美运行,但它是 Parent Class 中的注释,而不是我在您的帖子中看到的 Child 类。

Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐