Symfony和Doctrine中的多对多关系ON DELETE CASCADE

8

我希望在Symfony和Doctrine中实现一个简单的多对多关系。这实际上是一种单向的一对多关联,可以通过连接表进行映射,如文档所述。我正在使用一个YAML文件来配置它,以下是代码:

在Content.orm.yml文件中:

manyToMany:
  comments:
    cascade: ["persist","remove"]
    onDelete: CASCADE
    options:
      cascade:
        remove: true
        persist: true
        #refresh: true
        #merge: true
        #detach: true
    orphanRemoval: false
    orderBy: null
    targetEntity: Comment
    joinTable:
      name: content_comments
      joinColumns:
        content_id:
          referencedColumnName: id
      inverseJoinColumns:
        comment_id:
          referencedColumnName: id
          unique: true

这将生成以下SQL命令:

$ php app/console doctrine:schema:update --dump-sql | grep -i "comment\|content"
CREATE TABLE comment (id INT AUTO_INCREMENT NOT NULL, text LONGTEXT NOT NULL, content_id INT NOT NULL, creation_date DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE contents (id INT AUTO_INCREMENT NOT NULL, user INT DEFAULT NULL, user_id INT NOT NULL,file VARCHAR(255) DEFAULT NULL, INDEX IDX_B4FA11778D93D649 (user), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE content_comments (content_id INT NOT NULL, comment_id INT NOT NULL, INDEX IDX_D297CC584A0A3ED (content_id), UNIQUE INDEX UNIQ_D297CC5F8697D13 (comment_id), PRIMARY KEY(content_id, comment_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE contents ADD CONSTRAINT FK_B4FA11778D93D649 FOREIGN KEY (user) REFERENCES users (id);
ALTER TABLE content_comments ADD CONSTRAINT FK_D297CC584A0A3ED FOREIGN KEY (content_id) REFERENCES contents (id);
ALTER TABLE content_comments ADD CONSTRAINT FK_D297CC5F8697D13 FOREIGN KEY (comment_id) REFERENCES comment (id);

但是你可以看到,在FOREIGN KEY指令中没有"ON DELETE CASCADE"部分,即使我尝试使用所有找到的YAML注释。
因为在代码中,我正在尝试删除一个"content"实体及其关联的所有"comments"。
        $comments = $content->getComments();

        // Remove first the parent
        $entity_manager->remove($content);
        $entity_manager->flush();

        // Remove the childs
        foreach($comments as $comment)
        {
            $entity_manager->remove($comment);
        }

        $entity_manager->flush();

这会产生以下异常。
An exception occurred while executing 'DELETE FROM comment WHERE id = ?' with params [1]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`bb2server`.`content_comments`, CONSTRAINT `FK_D297CC5F8697D13` FOREIGN KEY (`comment_id`) REFERENCES `comment` (`id`))

那么,我做错了什么?或者如何强制Doctrine在多对多关系中使用"ON DELETE CASCADE"?

我的唯一肮脏的解决方法是删除SQL查询并重新构建自己,但我需要Doctrine在schema:update中创建查询以避免我的修补:

mysql> show create table content_comments;
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table            | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| content_comments | CREATE TABLE `content_comments` (
  `content_id` int(11) NOT NULL,
  `comment_id` int(11) NOT NULL,
  PRIMARY KEY (`content_id`,`comment_id`),
  UNIQUE KEY `UNIQ_D297CC5F8697D13` (`comment_id`),
  KEY `IDX_D297CC584A0A3ED` (`content_id`),
  CONSTRAINT `FK_D297CC584A0A3ED` FOREIGN KEY (`content_id`) REFERENCES `contents` (`id`),
  CONSTRAINT `FK_D297CC5F8697D13` FOREIGN KEY (`comment_id`) REFERENCES `comment` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> ALTER TABLE content_comments DROP FOREIGN KEY FK_D297CC5F8697D13;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE content_comments ADD CONSTRAINT FK_D297CC5F8697D13 FOREIGN KEY (`content_id`) REFERENCES `contents` (`id`) ON DELETE CASCADE;
Query OK, 10 rows affected (0.07 sec)
Records: 10  Duplicates: 0  Warnings: 0

编辑:解决方案。我需要将onDelete: CASCADE放在JoinColumns下面。

manyToMany:
  comments:
    cascade: ["persist","remove"]
    onDelete: CASCADE
    options:
      cascade:
        remove: true
        persist: true
        #refresh: true
        #merge: true
        #detach: true
    orphanRemoval: false
    orderBy: null
    targetEntity: Comment
    joinTable:
      name: content_comments
      joinColumns:
        content_id:
          referencedColumnName: id
          onDelete: CASCADE
      inverseJoinColumns:
        comment_id:
          referencedColumnName: id
          unique: true
          onDelete: CASCADE
3个回答

13

你需要在“joinColumns”和“inverseJoinColumns”中添加“JoinColumn”定义,像这样:

/**
 * @var Collection|null
 * @ManyToMany(targetEntity="...")
 * @JoinTable(name="...",
 *     joinColumns={@JoinColumn(name="`...`", referencedColumnName="id", onDelete="CASCADE")},
 *     inverseJoinColumns={@JoinColumn(name="`...`", referencedColumnName="id", onDelete="CASCADE")}
 * )
 */

在那里,您可以添加onDelete cascade(数据库级别)的定义。


8

我从未使用过YAML格式来定义我的实体和关系,因此我不知道它是否相同,但是使用注释时onDelete选项属于@ORM\JoinColumn注释:

/**
 * @var \AppBundle\Entity\Actor $actor
 *
 * @ORM\ManyToOne(targetEntity="Actor", inversedBy="fields")
 * @ORM\JoinColumn(name="actor_id", referencedColumnName="id", nullable=false, onDelete="cascade")
 */
protected $actor = null;

提示:在快速搜索后,我找到了您的答案:https://dev59.com/3F3Va4cB1Zd3GeqPDsQv#8330495


3
当使用@ORM\JoinTable而不是JoinColumn时,这对于多对多关系无效。 - FabienChn

0

我在我的yml Symfony配置中同时使用了onDelete: CASCADEcascade: ["remove"],尽管实体本身已经正确配置,但这导致了错误1451:

manyToMany:
    things:
        targetEntity: Thing
        joinTable:
            name: table
            joinColumns:
                variant_id:
                    referencedColumnName: id
            inverseJoinColumns:
                thing_id:
                    referencedColumnName: id
                    onDelete: CASCADE

        cascade: ["remove"]
        orphanRemoval: true

为了解决这个问题,我移除了 onDelete: CASCADE,只留下了 cascade: ["remove"]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接