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