1. <small id='XIFUV'></small><noframes id='XIFUV'>

    1. <tfoot id='XIFUV'></tfoot>
      <legend id='XIFUV'><style id='XIFUV'><dir id='XIFUV'><q id='XIFUV'></q></dir></style></legend>
      <i id='XIFUV'><tr id='XIFUV'><dt id='XIFUV'><q id='XIFUV'><span id='XIFUV'><b id='XIFUV'><form id='XIFUV'><ins id='XIFUV'></ins><ul id='XIFUV'></ul><sub id='XIFUV'></sub></form><legend id='XIFUV'></legend><bdo id='XIFUV'><pre id='XIFUV'><center id='XIFUV'></center></pre></bdo></b><th id='XIFUV'></th></span></q></dt></tr></i><div id='XIFUV'><tfoot id='XIFUV'></tfoot><dl id='XIFUV'><fieldset id='XIFUV'></fieldset></dl></div>
        <bdo id='XIFUV'></bdo><ul id='XIFUV'></ul>

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

      时间:2024-08-15
        <bdo id='rQnGz'></bdo><ul id='rQnGz'></ul>

        <small id='rQnGz'></small><noframes id='rQnGz'>

      • <legend id='rQnGz'><style id='rQnGz'><dir id='rQnGz'><q id='rQnGz'></q></dir></style></legend>
            <tbody id='rQnGz'></tbody>
            <tfoot id='rQnGz'></tfoot>

              1. <i id='rQnGz'><tr id='rQnGz'><dt id='rQnGz'><q id='rQnGz'><span id='rQnGz'><b id='rQnGz'><form id='rQnGz'><ins id='rQnGz'></ins><ul id='rQnGz'></ul><sub id='rQnGz'></sub></form><legend id='rQnGz'></legend><bdo id='rQnGz'><pre id='rQnGz'><center id='rQnGz'></center></pre></bdo></b><th id='rQnGz'></th></span></q></dt></tr></i><div id='rQnGz'><tfoot id='rQnGz'></tfoot><dl id='rQnGz'><fieldset id='rQnGz'></fieldset></dl></div>
                本文介绍了与 Symfony 和 Doctrine 的 ON DELETE CASCADE 的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想要与 Symfony 和 Doctrine 建立简单的多对多关系.这实际上是一个单向的一对多关联,可以通过连接表映射为 文档表明我正在使用 YAML 文件使用以下代码进行配置:

                I want a simple many to many relation with Symfony and Doctrine. This is really a unidirectional one-to-many association can be mapped through a join table as the docs indicate I am using a YAML file for configure this with the following code:

                在文件 Content.orm.yml 中:

                In file 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 命令:

                This produce the following SQL commands:

                $ 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 注释都放入.

                But as you can see, the FOREIGN KEY instructions doesn't have the parte "ON DELETE CASCADE", even I try to put all the YAML annotations that I found.

                因为在代码中,我试图删除一个内容"实体以及与此代码关联的所有评论":

                Because in code, I am trying to delete a "content" entity and all the "comments" associated with this code:

                        $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]:
                
                SQLSTATE[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"?

                So, what I am doing wrong? Or how to force to Doctrine to put "ON DELETE CASCADE" in many to many relations?

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

                My only dirty workaround it drop the SQL query and rebuild myself, but I need that Doctrine create the query in schema:update for avoid my patching:

                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 下

                workaround. I need to put the onDelete: CASCADE under the 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
                

                推荐答案

                我从来没有使用过 YAML 格式来定义我的实体和关系所以不知道是不是一样,但是带有注释的 onDelete 选项属于 @ORMJoinColumn 注释:

                I've never used the YAML format to define my entities and relations so I don't know if it is the same, but with annotations the onDelete option belongs to the @ORMJoinColumn annotation:

                /**
                 * @var AppBundleEntityActor $actor
                 *
                 * @ORMManyToOne(targetEntity="Actor", inversedBy="fields")
                 * @ORMJoinColumn(name="actor_id", referencedColumnName="id", nullable=false, onDelete="cascade")
                 */
                protected $actor = null;
                

                PS:经过快速搜索,我找到了您的答案:https://stackoverflow.com/a/8330495/5192753

                PS: After a quick search I've found your answer: https://stackoverflow.com/a/8330495/5192753

                这篇关于与 Symfony 和 Doctrine 的 ON DELETE CASCADE 的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Symfony 中覆盖捆绑的 Doctrine 存储库 下一篇:如何在 Doctrine 映射中描述多列外键

                相关文章

                <i id='taioT'><tr id='taioT'><dt id='taioT'><q id='taioT'><span id='taioT'><b id='taioT'><form id='taioT'><ins id='taioT'></ins><ul id='taioT'></ul><sub id='taioT'></sub></form><legend id='taioT'></legend><bdo id='taioT'><pre id='taioT'><center id='taioT'></center></pre></bdo></b><th id='taioT'></th></span></q></dt></tr></i><div id='taioT'><tfoot id='taioT'></tfoot><dl id='taioT'><fieldset id='taioT'></fieldset></dl></div>

                <small id='taioT'></small><noframes id='taioT'>

                  <bdo id='taioT'></bdo><ul id='taioT'></ul>
                1. <tfoot id='taioT'></tfoot>
                    <legend id='taioT'><style id='taioT'><dir id='taioT'><q id='taioT'></q></dir></style></legend>