symfony - Symfony2 Why Doctrine remove my element when I remove it from the collection? -
given have 2 entities, post , comment
my relations :
post
onetomany: comments: targetentity: comment mappedby: post orphanremoval: false cascade: [ persist ] fetch: extra_lazy
comment
manytoone: post: targetentity: post joincolumn: name: post_id referencedcolumnname: id ondelete: set null
now in controller, whenever want manually remove comment post, doctrine deletes comment database.
public function testaction() { $postrepository = $this->getdoctrine() ->getmanager() ->getrepository('mybundle:post'); $post = $postrepository ->find(1); $commentrepository = $this->getdoctrine() ->getmanager() ->getrepository('mybundle:comment'); $comment = $commentrepository ->find(1); /* comment child of post */ $post->removecomment($comment); } /* entity/post */ public function removecomment(comment $comment) { $this->comments->removeelement($comment); }
by doing that, comment deleted database, don't it.
ps : found solution avoid doctrine deleting entity :
foreach($post->getcomments() $_comment) { if ($commment === $_comment) { $post->removecomment($comment); } }
in case doctrine didn't deleted comment , removed relation want. can explain me please ?
thanks
Comments
Post a Comment