如何从Doctrine中的join-table(ManyToMany)中删除行?

时间:2022-10-21 22:35:19

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

我有一个通过在Symfony2 / Doctrine中使用@ORM \ ManyToMany注释创建的连接表。它加入了Category和Parameter表。

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:

现在我想删除Parameter表中的所有参数。因为在join-table上定义了外键约束,所以我不能只从Parameter表中删除行。首先,我必须从join-table中删除子行。但是Dotrine的DQL语法需要给出实体的名称,例如:

DELETE Project\Entity\EntityName

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

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

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

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

EDIT:

编辑:

join-table schema:

连接表模式:

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;

on UPDATE and on DELETE by default are set to RESTRICT

在UPDATE和默认情况下,DELETE设置为RESTRICT

THE FINAL SOLUTION WOULD BE:

最终解决方案将是:

 * @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")}) 

1 个解决方案

#1


30  

To set cascade on doctrine level:

要在学说级别设置级联:

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

More info: Doctrine2 Annotation Reference.

更多信息:Doctrine2注释参考。

To set cascade on mysql level:

要在mysql级别设置级联:

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

#1


30  

To set cascade on doctrine level:

要在学说级别设置级联:

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

More info: Doctrine2 Annotation Reference.

更多信息:Doctrine2注释参考。

To set cascade on mysql level:

要在mysql级别设置级联:

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