/** * Creates a new Comment entity. * * @Route("/new/{id}", name="comment_new") * @Method({"GET", "POST"}) */ public function newAction(Request $request, $id) { $em = $this->getDoctrine()->getManager(); $news = $em->getRepository('AppBundle:News')->find($id); if (!$news instanceof News) { throw new Exception("Error Processing Request", 1); } $comment = new Comment(); $comment->setNews($news); $form = $this->createForm(get_class(new CommentType()), $comment); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->get('event_dispatcher')->dispatch('pre_comment_created', new GenericEvent($comment)); $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); $this->get('event_dispatcher')->dispatch('post_comment_created', new GenericEvent($comment)); return $this->redirectToRoute('news_index'); } return $this->render('comment/new.html.twig', array('comment' => $comment, 'form' => $form->createView())); }