级联中的实体删除不能在ManyToMany关系中工作 [英] deleting entities in cascade not working in ManyToMany relation
问题描述
我要做什么 br>
当我删除一个类别时,我想要的是,类别中标记的每个文章也被删除,但只有当它们没有被其他类别标记时。
我已经测试了什么
我用2个类别(id = 1和id = 2)和两个文章(id = 71和id = 91)进行了测试。
第71条同时属于第1类和第2类。第91条仅与第2类相关联。
因此,删除第2类时,我希望删除第91条但不删除第71条(因为这仍然是与类别1相关联)
但是,没有一个发生在我尝试过的任何事情...
在我总结了使用不同的策略(1 / cascade = {remove},2 / orphanRemoval = true和3 / ondelete =CASCADE)。
下面的图片 >
在 绿色 中删除数据库。
我的代码(代码部分)
控制器
public function deleteCategory(Category $ category)
{
$ em = $ this-> getDoctrine() - > getManager();
$ em-> remove($ category);
$ em-> flush();
}
策略1 / cascade = {删除 }
// IN CLASS / ENTITY CATEGORY - 拥有者关系
* @ ORM\ManyToMany(targetEntity =Article,inversedBy =categories,cascade = {remove})
private $ articles;
// IN CLASS / ENTITY ARTICLE
* @ ORM\ManyToMany(targetEntity =Category,mappedBy =articles)
private $ categories;
为策略2 / orphanRemoval = true
// IN CLASS / ENTITY CATEGORY - 所有者的关系
* @ ORM\ManyToMany(targetEntity =文章,inversedBy =categories,orphanRemoval = true)
private $ articles;
// IN CLASS / ENTITY ARTICLE
* @ ORM\ManyToMany(targetEntity =Category,mappedBy =articles)
private $ categories;
策略3 / onDelete =CASCADE em>
// IN CLASS / ENTITY CATEGORY - 所有者的关系
* @ ORM\ManyToMany(targetEntity =Article,inversedBy =categories)
private $ articles;
// IN CLASS / ENTITY ARTICLE
* @ ORM\ManyToMany(targetEntity =Category,mappedBy =articles)
* @ ORM\JoinColumn(onDelete =CASCADE)
private $ categories;
我的问题
还有其他一些方法,但是我已经尝试过的3种策略或者这些移除必须在控制器中完成?
只要我能想到,您就不能使用级联功能。
一个干净的解决方案是创建一个预订 preRemove
事件的监听器。
然后,您可以检查一个类别是否被删除,并查找该类别中没有其他类别的所有文章,并删除这些类别。
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.
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...
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.
MY CODE (PART OF CODE)
in the controller
public function deleteCategory(Category $category)
{
$em = $this->getDoctrine()->getManager();
$em->remove($category);
$em->flush();
}
for strategy 1/ cascade={"remove"}
// IN CLASS/ENTITY CATEGORY - OWNER OF THE RELATION
* @ORM\ManyToMany(targetEntity="Article", inversedBy="categories", cascade={ "remove"})
private $articles;
// IN CLASS/ENTITY ARTICLE
* @ORM\ManyToMany(targetEntity="Category", mappedBy="articles")
private $categories;
for strategy 2/ orphanRemoval=true
// IN CLASS/ENTITY CATEGORY - OWNER OF THE RELATION
* @ORM\ManyToMany(targetEntity="Article", inversedBy="categories", orphanRemoval=true)
private $articles;
// IN CLASS/ENTITY ARTICLE
* @ORM\ManyToMany(targetEntity="Category", mappedBy="articles")
private $categories;
for strategy 3/ onDelete="CASCADE"
// IN CLASS/ENTITY CATEGORY - OWNER OF THE RELATION
* @ORM\ManyToMany(targetEntity="Article", inversedBy="categories")
private $articles;
// IN CLASS/ENTITY ARTICLE
* @ORM\ManyToMany(targetEntity="Category", mappedBy="articles")
* @ORM\JoinColumn(onDelete="CASCADE")
private $categories;
MY QUESTION
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.
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.
这篇关于级联中的实体删除不能在ManyToMany关系中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!