本文介绍了如何从学说中的联接表(ManyToMany)中删除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个联接表,该联接表是通过使用Symfony2/Doctrine中的 @ORM \ ManyToMany 注释创建的.它连接 Category Parameter 表.

I have a join-table which is created by using @ORM\ManyToMany annotation in Symfony2/Doctrine.It joins Category and Parameter table.

现在,我想从参数"表中删除所有参数.因为在联接表上定义了外键约束,所以我不能只是从参数表中删除行.首先,我必须从联接表中删除子行.但是Dotrine的DQL语法要求提供实体名称,例如:

Now I want to delete all parameters from the Parameter table. Because there are foreign key constraints defined on join-table I can't just delete rows from Parameter table. First I have to delete child rows from join-table. But Dotrine's DQL syntax require to give a name of the entity, like:

DELETE Project\Entity\EntityName

但是使用ManyToMany关联生成的联接表实体的名称是什么?如何处理?

But what is the name of the join-table entity generated by using ManyToMany association? How to deal with it?

或者,如何在由 @ORM \ ManyToMany 注释定义的联接表中的外键约束上设置ON UPDATE CASCADE和ON DELETE CASCADE.

Alternately, how can I set ON UPDATE CASCADE and ON DELETE CASCADE on foreign key constraints in join-table defined by @ORM\ManyToMany annotation.

联接表模式:

CREATE TABLE `categories_params` (
    `category_id` INT(11) NOT NULL,
    `param_id` INT(11) NOT NULL,
    PRIMARY KEY (`category_id`, `param_id`),
    INDEX `IDX_87A730CB12469DE2` (`category_id`),
    INDEX `IDX_87A730CB5647C863` (`param_id`),
    CONSTRAINT `categories_params_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `allegro_category` (`id`),
    CONSTRAINT `categories_params_ibfk_2` FOREIGN KEY (`param_id`) REFERENCES `category_param` (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

默认情况下,

在UPDATE上在DELETE 上设置为 RESTRICT

on UPDATE and on DELETE by default are set to RESTRICT

最终解决方案将是:

 * @ORM\ManyToMany(targetEntity="CategoryParam", cascade={"persist","remove"})
 * @ORM\JoinTable(name="categories_params",
 *      joinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="param_id", referencedColumnName="id", onDelete="CASCADE")}) 

推荐答案

要在理论级别上设置级联:

To set cascade on doctrine level:

@ORM\ManyToMany(targetEntity="Target", inversedBy="inverse", cascade={"remove", "persist"})

更多信息: Doctrine2注释参考.

要在mysql级别上设置级联:

To set cascade on mysql level:

@ORM\JoinColumn(onDelete="CASCADE", onUpdate="CASCADE")

这篇关于如何从学说中的联接表(ManyToMany)中删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 12:48