public function addComment(Comment $comment) { $this->comments[] = $comment; //On lie de l'autre côté aussi : $comment->setUser($this); return $this; }
public function load(ObjectManager $manager) { for ($i = 1; $i < 151; $i++) { $comment = new Comment(); $comment->setUser('Hans Meier'); $comment->setComment('Huh so funny'); if ($i % 2 == 0) { $blog = 'blog-1'; } else { $blog = 'blog-2'; } $comment->setBlog($this->getReference($blog)); $manager->persist($comment); } $manager->flush(); }
public function readAction(Post $post, Request $request) { $read_post = $this->getDoctrine()->getRepository('BlogBundle:Post')->find($post); $comment = new Comment(); $comment->setDateComment(new \DateTime('tomorrow')); $actual_user_name = $this->get('security.token_storage')->getToken()->getUsername(); $comment->setAuthorComment($actual_user_name); $user_implement = $this->getDoctrine()->getRepository('BlogBundle:User')->findOneBy(array('username' => $actual_user_name)); $comment->setInPost($post); $comment->setUser($user_implement); $form_comment = $this->createFormBuilder($comment)->add('textComment', 'textarea', array('label' => 'Your comment', 'attr' => array('rows' => 20, 'cols' => 88)))->getForm(); $form_comment->handleRequest($request); if ($form_comment->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); return $this->redirect($this->generateUrl('read_post', array('post' => $post->getId()))); } $comments = $read_post->getComments(); return $this->render('@Blog/Page_templates/read.html.twig', array('read_post' => $read_post, 'form_for_comment' => $form_comment->createView(), 'comments' => $comments, 'username' => $actual_user_name)); }