/** * Comment List * * @param String $slug * @return Response * */ public function commentListAction($slug) { $translator = $this->get('translator'); $em = $this->getDoctrine()->getManager(); // get article $article = $em->getRepository('NewsBundle:Article')->findOneBy(array('slug' => $slug)); //findBySlug($slug); if (!$article) { throw $this->createNotFoundException($translator->trans('AdministrationBundle.notFound', array("%name%" => "article"))); //'Unable to find Type entity.'); } // get lang $lang = $this->get('request')->getLocale(); if (!$lang) { $lang = "en"; } // get user $user = $this->getUser(); // get comment $comment = $this->get('request')->get('text'); // add comment if ($comment) { $entity = new Comment(); $entity->setArticle($article); $entity->setUser($user); $entity->setLang($lang); $entity->setComment($comment); $em->persist($entity); $em->flush(); } // get all comments $comments = $em->getRepository('EventBundle:Comment')->getCommentsByArticle($article, $lang); $reponse = array(); foreach ($comments as $key => $comment) { # code... $reponse[$key]['author'] = $comment->getUser()->getFirstname() . " " . $comment->getUser()->getLastname(); $reponse[$key]['text'] = $comment->getComment(); if ($lang == "en") { $reponse[$key]['date'] = $comment->getCommentDate()->format('Y-m-d H:i'); } else { $reponse[$key]['date'] = $comment->getCommentDate()->format('d/m/Y H:i'); } } return new JsonResponse($reponse); }