Example #1
0
 /**
  * Create new Comment
  * @return Response
  */
 public function newCommentAction()
 {
     $em = $this->getDoctrine()->getManager();
     parse_str($_POST['formData'], $formData);
     $text = $formData['text'];
     $post = $em->getRepository('AppBundle:Post')->find($_POST['postId']);
     $response = array();
     try {
         $comment = new PostComment();
         $comment->setPost($post);
         $comment->setText($text);
         $comment->setUser($this->getUser());
         # Validation
         $validator = $this->get('validator');
         $errors = $validator->validate($comment);
         $errorsString = (string) $errors;
         if (count($errors) > 0) {
             $response['error'] = $errorsString;
         } else {
             $em->persist($comment);
             $em->flush();
             $response['comment'] = array('createdAt' => date('d.m.Y. H:i', $comment->getCreatedAt()->getTimestamp()), 'text' => $comment->getText(), 'user' => array('id' => $comment->getUser()->getId(), 'username' => $comment->getUser()->getUsername()));
         }
     } catch (\Exception $e) {
         $response['error'] = $e->getMessage();
     }
     return new Response(json_encode($response));
 }
 /**
  * Remove comment
  *
  * @param \AppBundle\Entity\PostComment $comment
  */
 public function removeComment(\AppBundle\Entity\PostComment $comment)
 {
     $this->comments->removeElement($comment);
 }