本文介绍了学说ManyToMany:删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在Doctrine中遇到单向ManyToMany关系的问题.情况非常简单:产品具有许多标签.标签可以附加到产品,也可以附加到我模型中的任何可标记"实体.这是我的代码段:I have got problem with unidirectional ManyToMany relationship in Doctrine. The case is very easy: Product has many Tags. Tag can be attached to Product but also to any "taggable" entity in my model. Here is snippet of my code:/** * @Entity * @Table(name="products") **/class Product { /** some other fields here */ /** * @ManyToMany(targetEntity="Tag") * @JoinTable(name="products_tags", * joinColumns={@JoinColumn(name="product_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="id")} * ) */ protected $tags;}由于省略了Tag类的单向关系代码.Since its unidirectional relation code of Tag class is omitted.对于这样定义的关联,Doctrine生成了以下SQL代码(产品表和标签表的SQL被跳过):For such defined association Doctrine generated the following SQL code (SQL for products table and tags table is skipped): CREATE TABLE `products_tags` ( `product_id` int(11) NOT NULL, `tag_id` int(11) NOT NULL, PRIMARY KEY (`product_id`,`tag_id`), KEY `IDX_E3AB5A2C4584665A` (`product_id`), KEY `IDX_E3AB5A2CBAD26311` (`tag_id`), CONSTRAINT `FK_E3AB5A2CBAD26311` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`), CONSTRAINT `FK_E3AB5A2C4584665A` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |我要删除已附加一些标签的产品.I would like to remove product that has some tags attached to it./* $product is already persisted, $em is an Entity Manager */$em->remove($product);$em->flush();它显然是由于违反完整性约束而失败(无法删除或更新父行:外键约束失败( products_tags ,CONSTRAINT FK_E3AB5A2CBAD26311 FOREIGN KEY( tag_id )参考标签( id ))').It obviously fails due to integrity constraint violation ("Cannot delete or update a parent row: a foreign key constraint fails (products_tags, CONSTRAINT FK_E3AB5A2CBAD26311 FOREIGN KEY (tag_id) REFERENCES tags (id))'"). 当我更改products_tags表并将ON DELETE CASCADE添加到外键时,它可以按我的要求工作.我可以轻松地删除TAG($ em-> remove($ tag))和PRODUCT($ em-> remove($ product),它们会自动从products_tags表中删除引用的行.When I alter products_tags table adding ON DELETE CASCADE to foreign keys it works as I want. I can EASILY remove TAG ($em->remove($tag)) and PRODUCT ($em->remove($product) that automatically removes referenced rows from products_tags table. 如何使用ON CASCADE DELETE外键获取product_tags表,我的代码应该如何?我已经对cascade = {"all"}感到厌倦,但是失败了.How my code should look like to obtain products_tags table with ON CASCADE DELETE foreign keys? I've already tired with cascade={"all"} but it failed. 我知道,我可以从产品的标签集合中删除所有标签,但是正如我提到的,我想一步实现,只需调用实体管理器对象的remove方法即可.I know, I can remove all tag from product's tags collection, but as I mentioned I would like to achieve it in one step, just by calling remove method of entity manager object.学说真的缺乏吗?推荐答案好,我通过挖掘Doctrine2文档来管理自己;)解决方案是将 onDelete ="cascade" 添加到 @JoinColumn .Ok, I managed myself by digging in Doctrine2 docs ;) Solution is to add onDelete="cascade" to @JoinColumn./** * @Entity * @Table(name="products") **/class Product { /** some other fileds here */ /** * @ManyToMany(targetEntity="Tag") * @JoinTable(name="products_tags", * joinColumns={@JoinColumn(name="product_id", referencedColumnName="id", onDelete="cascade")}, * inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")} * ) */ protected $tags;}请注意,cascade = {"all"}是在对象级别(在您的应用程序中)管理的,而onDelete ="cascade"是在数据库级别管理的.Note that, cascade={"all"} is managed on object level (in your app), while onDelete="cascade" is on database level. 这篇关于学说ManyToMany:删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 16:12