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 addComment(Comment $comment) { $this->comments[] = $comment; //On lie de l'autre côté aussi : $comment->setPost($this); return $this; }
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())); } }
/** * @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; }