Skip to content

Commit

Permalink
minor #529 Use Post::addComment method to associate new comment with …
Browse files Browse the repository at this point in the history
…a post (voronkovich)

This PR was squashed before being merged into the master branch (closes #529).

Discussion
----------

Use Post::addComment method to associate new comment with a post

Commits
-------

dce1e81 Use Post::addComment method to associate new comment with a post
  • Loading branch information
javiereguiluz committed May 8, 2017
2 parents 67fa51c + dce1e81 commit d90e0c1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/AppBundle/Controller/BlogController.php
Expand Up @@ -94,7 +94,8 @@ public function commentNewAction(Request $request, Post $post)
{
$comment = new Comment();
$comment->setAuthor($this->getUser());
$comment->setPost($post);

$post->addComment($comment);

$form = $this->createForm(CommentType::class, $comment);

Expand Down
4 changes: 2 additions & 2 deletions src/AppBundle/DataFixtures/ORM/PostFixtures.php
Expand Up @@ -66,10 +66,10 @@ public function load(ObjectManager $manager)
$comment->setAuthor($this->getReference('john-user'));
$comment->setPublishedAt(new \DateTime('now + '.($i + $j).'seconds'));
$comment->setContent($this->getRandomCommentContent());
$comment->setPost($post);

$manager->persist($comment);
$post->addComment($comment);

$manager->persist($comment);
}

$manager->persist($post);
Expand Down
10 changes: 7 additions & 3 deletions src/AppBundle/Entity/Post.php
Expand Up @@ -203,13 +203,17 @@ public function getComments()

public function addComment(Comment $comment)
{
$this->comments->add($comment);
$comment->setPost($this);
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setPost($this);
}
}

public function removeComment(Comment $comment)
{
$this->comments->removeElement($comment);
if ($this->comments->removeElement($comment)) {
$comment->setPost(null);
}
}

public function getSummary()
Expand Down

0 comments on commit d90e0c1

Please sign in to comment.