Skip to content

Commit 8017175

Browse files
committed
Improve Post::addComment and Post::removeComment methods
1 parent 0db7504 commit 8017175

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/AppBundle/Entity/Post.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,20 @@ public function getComments()
227227
*/
228228
public function addComment(Comment $comment)
229229
{
230-
$this->comments->add($comment);
231-
$comment->setPost($this);
230+
if (!$this->comments->contains($comment)) {
231+
$this->comments->add($comment);
232+
$comment->setPost($this);
233+
}
232234
}
233235

234236
/**
235237
* @param Comment $comment
236238
*/
237239
public function removeComment(Comment $comment)
238240
{
239-
$this->comments->removeElement($comment);
241+
if ($this->comments->removeElement($comment)) {
242+
$comment->setPost(null);
243+
}
240244
}
241245

242246
/**

0 commit comments

Comments
 (0)