php - Many to many relation with ON DELETE CASCADE with Symfony and Doctrine -
i want simple many many relation symfony , doctrine. unidirectional one-to-many association can mapped through join table the docs indicate using yaml file configure following code:
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
this produce 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);
but can see, foreign key instructions doesn't have parte "on delete cascade", try put yaml annotations found.
because in code, trying delete "content" entity , "comments" associated code:
$comments = $content->getcomments(); // remove first parent $entity_manager->remove($content); $entity_manager->flush(); // remove childs foreach($comments $comment) { $entity_manager->remove($comment); } $entity_manager->flush();
this produce following exception.
an exception occurred while executing 'delete comment id = ?' params [1]:\n\nsqlstate[23000]: integrity constraint violation: 1451 cannot delete or update parent row: foreign key constraint fails (`bb2server`.`content_comments`, constraint `fk_d297cc5f8697d13` foreign key (`comment_id`) references `comment` (`id`))
so, doing wrong? or how force doctrine put "on delete cascade" in many many relations?
my dirty workaround drop sql query , rebuild myself, need doctrine create query in schema:update avoid 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
edit: workaround. need put ondelete: cascade under 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
i've never used yaml
format define entities , relations don't know if same, annotations ondelete
option belongs @orm\joincolumn
annotation:
/** * @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;
ps: after quick search i've found answer: https://stackoverflow.com/a/8330495/5192753
Comments
Post a Comment