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 $em) { $comment_blog = new Comment(); $comment_blog->setPost($em->merge($this->getReference('post-blog'))); $comment_blog->setUsername('admin'); $comment_blog->setAuthorEmail('*****@*****.**'); $comment_blog->setWebsite('www.demo.pl'); $comment_blog->setContent('YouTube has become the standard way for delivering high quality video on the web.'); //$comment_blog->setIsApproved(0); $comment_shop = new Comment(); $comment_shop->setPost($em->merge($this->getReference('post-shop'))); $comment_shop->setUsername('journalist'); $comment_shop->setAuthorEmail('*****@*****.**'); $comment_shop->setContent('Bootstrap is the most widely used frontend framework right now.'); $em->persist($comment_blog); $em->persist($comment_shop); foreach (range(0, 60) as $i) { $comment = new Comment(); $comment->setPost($em->merge($this->getReference('post-post' . rand(0, 30)))); //mamy 30 postów dlatego rand(0, 30) $surfer = $this->getRandomSurfer(); $comment->setUsername($surfer['username']); $comment->setAuthorEmail($surfer['author_email']); $comment->setWebsite($this->getRandomWebsite()); $comment->setContent($this->getRandomContent()); $comment->setIsApproved(rand(0, 1) == 0 ? 0 : 1); $comment->setPublishedAt(new \DateTime('now - ' . rand(1, 4) . 'days')); $em->persist($comment); } $em->flush(); $this->addReference('comment-blog', $comment_blog); $this->addReference('comment-shop', $comment_shop); }
public function createAction(Request $request, $blog_id) { $blog = $this->getBlog($blog_id); $comment = new Comment(); $comment->setBlog($blog); $form = $this->createForm(CommentType::class, $comment); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); return $this->redirect($this->generateUrl('blog_show', array('id' => $comment->getBlog()->getId())) . '#comment-' . $comment->getId()); } return $this->render(':Comment:create.html.twig', array('comment' => $comment, 'form' => $form->createView())); }
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 viewAction(Request $request, $id) { $repositoryArticle = $this->getDoctrine()->getRepository('BlogBundle:Article'); $repositoryComment = $this->getDoctrine()->getRepository('BlogBundle:Comment'); $article = $repositoryArticle->getById($id); $comments = $repositoryComment->getByArticle($article); $comment = new Comment(); $comment->setTarget($article); $comment->setPostDate(new \DateTime()); $form = $this->createform(CommentType::class, $comment); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush($comment); return $this->redirectToRoute('blog_view', ['id' => $article->getId()]); } return $this->render('BlogBundle:Article:view.html.twig', array('article' => $article, 'comments' => $comments, 'form' => $form->createView())); }
public function showAction($id, Request $request) { $post = $this->getDoctrine()->getRepository('BlogBundle:Post')->find($id); if (!$post) { throw $this->createNotFoundException('Brak postu o id ' . $id); } $comment = new Comment(); $comment->setPost($post); $comment->setDate(new \DateTime('now')); $form = $this->createFormBuilder($comment)->add('author', TextType::class, array('label' => 'Imię'))->add('content', TextareaType::class, array('label' => 'Treść'))->add('save', SubmitType::class, array('label' => 'Dodaj komentarz'))->getForm(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); $this->addFlash('notice', 'Twój komentarz został dodany!'); return $this->redirectToRoute('blog_showpost', array('id' => $id)); } else { return $this->render('BlogBundle:Default:showpost.html.twig', array('post' => $post, 'form' => $form->createView())); } }
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)); }
/** * @param Comment $comment * @param Request $request * * @throws ItemResolvingException * @return CartItemInterface|void */ public function resolve(Comment $comment, Request $request) { $em = $this->getDoctrine()->getManager(); $postId = $request->query->get('post'); $itemForm = $request->get('comment_front'); if ($this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { $comment->setActor($this->container->get('security.token_storage')->getToken()->getUser()); } else { //register user $actor = $this->createActor($itemForm); $comment->setActor($actor); } $postRepository = $em->getRepository('BlogBundle:Post'); if (!$postId || !($post = $postRepository->find($postId))) { // no product id given, or product not found throw new \Exception('Requested post was not found'); } // assign the product and quantity to the item $comment->setPost($post); $comment->setComment($itemForm['comment']); return $comment; }
public function isMyComment(Comment $comment) { $myComments = $this->session->get('sessionComments', false); if (!$myComments) { return false; } else { $myComments = unserialize($myComments); } return in_array($comment->getId(), $myComments); }
/** * Creates a form to delete a Comment entity. * * @param Comment $comment The Comment entity * * @return \Symfony\Component\Form\Form The form */ private function createDeleteForm(Comment $comment) { return $this->createFormBuilder()->setAction($this->generateUrl('blog_comment_delete', array('id' => $comment->getId())))->setMethod('DELETE')->getForm(); }