Skip to content

Commit 1dfa804

Browse files
committed
minor #559 RFC: Always enforce updating inverse side for Comment (bocharsky-bw)
This PR was merged into the master branch. Discussion ---------- RFC: Always enforce updating inverse side for Comment It's an edge case, but what do you think about always updating inverse side in adder/remover methods? For example, currently this code won't update inverse side properly: ```php $comment = new Comment(); $post->getComments()->add($comment); // but then if we will call adder method $post->addComment($comment); // Comment::$post is still equals null here due to the failed `!contains()` check in it ``` Commits ------- b5342c1 Always enforce updating inverse side for Comment
2 parents 5980f4b + b5342c1 commit 1dfa804

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

src/AppBundle/Entity/Post.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,16 @@ public function getComments()
203203

204204
public function addComment(Comment $comment)
205205
{
206+
$comment->setPost($this);
206207
if (!$this->comments->contains($comment)) {
207208
$this->comments->add($comment);
208-
$comment->setPost($this);
209209
}
210210
}
211211

212212
public function removeComment(Comment $comment)
213213
{
214-
if ($this->comments->removeElement($comment)) {
215-
$comment->setPost(null);
216-
}
214+
$comment->setPost(null);
215+
$this->comments->removeElement($comment);
217216
}
218217

219218
public function getSummary()

0 commit comments

Comments
 (0)