/**
  * @Route("/album/{id}/comment", requirements={
  *     "id": "\d+"
  * })
  * @Method({"POST", "OPTIONS"})
  */
 public function commentAlbumAction(Request $request, Album $album)
 {
     $this->denyAccessUnlessGranted('comment', $album, 'You are not allowed to comment this album.');
     $text = $request->get('text');
     $comment = new AlbumComment();
     $comment->setText($text);
     $comment->setAuthor($this->getUser());
     $comment->setDate(new \DateTime());
     $album->addComment($comment);
     $validator = $this->get('validator');
     $errors = $validator->validate($album);
     if (count($errors) > 0) {
         return new JsonResponse(Util::violationListToJson($errors));
     }
     $em = $this->getDoctrine()->getManager();
     $em->persist($comment);
     $em->persist($album);
     $em->flush();
     return new JsonResponse($album->toJson());
 }
示例#2
0
 /**
  * Add comment
  *
  * @param \AppBundle\Entity\AlbumComment $comment
  *
  * @return Album
  */
 public function addComment(AlbumComment $comment)
 {
     $this->comments[] = $comment;
     $comment->setAlbum($this);
     return $this;
 }