级联删除实体在多对多关系中不起作用 [英] deleting entities in cascade not working in ManyToMany relation

查看:14
本文介绍了级联删除实体在多对多关系中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父实体类别和一个子实体文章.它们由 ManyToMany 关系定义.一篇文章可以标记在一个或多个类别中,每个类别可以标记在多个文章中.

I have a parent entity Category and a child entity Article. They are defined by a ManyToMany relation. One article can be taged in one or more category and every Category can be tagged in more than one Article.

我想做什么
我希望当我删除一个类别时,该类别中标记的每篇文章也会被删除,但前提是它们没有被其他类别标记.

WHAT I TRY TO DO
I would like that when I delete a Category, every Article tagged in the Category are also deleted BUT only if they are not tagged by an other Category.

我已经测试过的内容
我测试了 2 个类别(id=1 和 id=2)和两篇文章(id=71 和 id=91).第 71 条同时具有第 1 类和第 2 类.第 91 条仅与第 2 类相关.
因此,在删除第 2 类时,我希望删除第 91 条而不是第 71 条(因为这一篇仍然与 category_1 链接)

WHAT I HAVE ALREADY TESTED
I tested with 2 category (id=1 and id=2) and with two articles (id=71 and id=91). article 71 has both category 1 and 2. Article 91 is linked only with category 2.
So when deleting category 2 I expect to delete article 91 but not article 71 (as this one is still linked with category_1)

但是无论我尝试什么都没有发生...

But none of this happenned whatever I tried...

下图中,我总结了使用不同策略(1/cascade={"remove"}、2/orphanRemoval=true 和 3/ondelete="级联").
绿色从数据库中删除.

In the following picture I summed up what I get using different strategies (1/cascade={"remove"} , 2/orphanRemoval=true and 3/ondelete="CASCADE" ).
In green the removal from the database.

我的代码(部分代码)

在控制器中

public function deleteCategory(Category $category)
{
  $em = $this->getDoctrine()->getManager();
  $em->remove($category);
  $em->flush();
}

对于策略 1/cascade={"remove"}

 // IN CLASS/ENTITY CATEGORY - OWNER OF THE RELATION
 * @ORMManyToMany(targetEntity="Article", inversedBy="categories",  cascade={ "remove"})
 private $articles;

 // IN CLASS/ENTITY ARTICLE 
 * @ORMManyToMany(targetEntity="Category", mappedBy="articles")
 private $categories;

对于策略 2/orphanRemoval=true

 // IN CLASS/ENTITY CATEGORY - OWNER OF THE RELATION
 * @ORMManyToMany(targetEntity="Article", inversedBy="categories",  orphanRemoval=true)
 private $articles;

 // IN CLASS/ENTITY ARTICLE 
 * @ORMManyToMany(targetEntity="Category", mappedBy="articles")
 private $categories;

对于策略 3/onDelete="CASCADE"

 // IN CLASS/ENTITY CATEGORY - OWNER OF THE RELATION
 * @ORMManyToMany(targetEntity="Article", inversedBy="categories")
 private $articles;

 // IN CLASS/ENTITY ARTICLE 
 * @ORMManyToMany(targetEntity="Category", mappedBy="articles")
 * @ORMJoinColumn(onDelete="CASCADE")

 private $categories;

我的问题

除了我已经尝试过的 3 种策略或者必须在控制器中完成这些删除之外,还有其他方法吗?

is there some other way of doing it but the 3 strategies I already tried or those removals have to be done in the controller?

推荐答案

据我所知,您不能仅使用级联功能来做到这一点.

You can't do this using just cascading features as far as I can think of.

一个干净的解决方案是创建一个订阅 preRemove 事件的侦听器.然后,您可以检查某个类别何时被删除,并查找该类别中没有任何其他类别的所有文章并将其删除.

A clean solution would be to create an listener which subscribes to the preRemove events. You can then check when a category is deleted and find all articles from that category which do not have any other categories and remove those as well.

这篇关于级联删除实体在多对多关系中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆