Doctrine MongoDB delete 仅参考 onRemove
问题:Doctrine MongoDB delete 仅参考 onRemove
我在 ChartPage 和 BaseChart 之间有 OneToMany 关系:
1 ChartPage holds 1 BaseChart
和1 BaseChart holds many ChartPages
图表在我的应用程序的不同包中进行管理,因此可以单独删除它们。我喜欢的是,当删除图表时,Doctrine 会自动删除 ChartPage.Chart 引用,但没有其他内容(不删除 ChartPage)。
反过来应该让一切保持原样:当我删除带有引用的 BaseChart 的 ChartPage 时 - 什么都不会发生(不删除 BaseChart)
我尝试了其中一个的所有组合:我能想到的cascade="{detach,merge,refresh,remove,persist}"
,但我想不出来..
这是我的映射:
<?php
/**
* Class ChartPage
* @package VBCMS\Bundle\AdminBundle\Document\Page
* @Serializer\AccessType("public_method")
* @MongoDB\Document()
*/
class ChartPage extends BasePage {
/**
* @var BaseChart
* @Serializer\Type("VBCMS\Bundle\StatisticBundle\Document\BaseChart")
* @Serializer\Accessor(setter="setChartDeserialize")
* @MongoDB\ReferenceOne(
* targetDocument="VBCMS\Bundle\StatisticBundle\Document\BaseChart",
* mappedBy="pages",
* cascade={"persist,detach,merge"}
* )
*/
protected $chart;
}
/
/**
* Class BaseChart
* @package VBCMS\Bundle\StatisticBundle\Document
* @Serializer\AccessType("public_method")
* @MongoDB\Document(
* collection="Chart",
* repositoryClass="VBCMS\Bundle\StatisticBundle\Repository\ChartRepository"
* )
*/
class BaseChart {
/**
* @var BasePage[]|Collection
* @Serializer\Exclude()
* @MongoDB\ReferenceMany(
* targetDocument="VBCMS\Bundle\AdminBundle\Document\Page\ChartPage",
* inversedBy="chart",
* cascade={"persist,detach,merge"}
* )
*/
protected $pages;
}
我剩下的唯一想法是构建一个自定义的 preRemove EventListener,在删除 BasePage 之前将引用设置回NULL
,但我希望我可以避免这种手动混乱。
解答
Doctrine MongoDB ODM 的级联功能只在一个方向上运行。如果您在对象 A 上执行一些生命周期事件,该对象具有对 B 的引用,我们可以将持久/删除/等级联到 B。ODM 中有一个孤立删除的概念,它允许自动删除嵌入或引用的对象在一对一或一对多的关系中。我不相信它记录在 ODM 手册中,但它与ORM 文档中描述的功能非常相似。
在您的情况下,您不希望在删除 A 时使用任何级联功能;您希望 B 保持原样。
另一方面,当您手动删除 B 对象时,您希望清除 A 对象中对 B 的所有引用。使用 pre 或 postRemove 侦听器是您最好的选择,并且如果您已索引 A 上的引用,那么将引用设置为null
应该是一个非常简单的多更新查询,它们曾经引用 B 的实例删除。
更多推荐
所有评论(0)