/** * @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()); }