/** * @param ObjectManager $manager */ public function load(ObjectManager $manager) { $comment = new Comment(); $comment->setAuthor($this->getReference('user')); $comment->setContent('Commentaire babla bla bla Beer'); $comment->setArticle($this->getReference('article')); $manager->persist($comment); $manager->flush(); $this->addReference('comment', $comment); }
public function addNewCommentToArticle($article, Comment $comment, $author) { if ($author !== null) { $comment->setAuthor($author); } $comment->setArticle($article); $em = $this->doctrine->getManager(); $em->persist($comment); $em->flush(); }
/** * @param Request $request * @param $slug * @return \Symfony\Component\HttpFoundation\RedirectResponse * @Route("/show/{slug}", name="add_comment") * @Method("POST") * @Template("AppBundle:Blog:article.html.twig") */ public function addCommentAction(Request $request, $slug) { if (!$this->get('security.authorization_checker')->isGranted('ROLE_USER')) { throw $this->createAccessDeniedException(); } $user = $this->getUser(); $repository = $this->getDoctrine()->getRepository('AppBundle:Article'); $article = $repository->findArticleBySlug($slug); $comment = new Comment(); $comment->setUser($user); $comment->setArticle($article[0]); $form = $this->createForm(CommentType::class, $comment, ['action' => $this->generateUrl('add_comment', ['slug' => $slug]), 'method' => 'POST'])->add('save', SubmitType::class, ['label' => 'Add comment']); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); return $this->redirect($this->generateUrl('show_article', ['slug' => $comment->getArticle()->getSlug()])); } return ['article' => $article, 'form' => $form->createView()]; }
public function load(ObjectManager $manager) { $faker = Factory::create(); for ($i = 1; $i <= 24; $i++) { $article = new Article(); // $article->setSlug('article' . $i); $article->setSummary($faker->sentences(10, true)); $article->setContent($faker->text(3000)); $article->setTitle($faker->sentence()); $slug = $this->container->get('app.slugger')->slugify($article->getTitle()); $article->setSlug($slug); $article->setAuthorEmail('*****@*****.**'); $article->setPathToImage('images/foto_' . $i . '.jpg'); for ($j = 1; $j <= 5; $j++) { $comment = new Comment(); $comment->setContent($faker->sentences(5, true)); //$comment->setPublishedAt(new \DateTime('now')); $comment->setArticle($article); $comment->setAuthorEmail('*****@*****.**'); $article->addComment($comment); $manager->persist($comment); } $manager->persist($article); $arrayId = array(); $k = rand(1, 10); while (count($arrayId) < $k) { $id = rand(1, 50); if (array_search($id, $arrayId) === false) { $arrayId[] = $id; $tagFromBase = $this->getReference("tag {$id}"); $article->addTag($tagFromBase); $tagFromBase->getArticles()->add($article); } } } $manager->flush(); }
public function addComment(Request $request, $slug, $id = null) { $em = $this->doctrine->getManager(); $article = $em->getRepository("AppBundle:Article")->findOneBySlug($slug); if ($id != null) { $comment = $em->getRepository('AppBundle:Comment')->find($id); } else { $comment = new Comment(); $comment->setArticle($article); $user = $this->tokenStorage->getToken()->getUser(); $comment->setUser($user); } $form = $this->formFactory->create(CommentType::class, $comment, ['em' => $em, 'action' => $this->router->generate('commentForm', ['slug' => $slug, 'id' => $id]), 'method' => Request::METHOD_POST]); $form->add('save', SubmitType::class, array('label' => 'Submit Comment', 'attr' => array('class' => "btn btn-primary"))); if ($request->getMethod() == 'POST') { $form->handleRequest($request); if ($form->isValid()) { $em->persist($comment); $em->flush(); return new RedirectResponse($this->router->generate('success')); } } return ['form' => $form->createView()]; }
/** * Add comments * * @param \AppBundle\Entity\Comment $comments * @return Article */ public function addComment(\AppBundle\Entity\Comment $comment) { $this->comments[] = $comment; $comment->setArticle($this); return $this; }
/** * Add comment * * @param Comment $comment * * @return Article */ public function addComment(Comment $comment) { $this->comments[] = $comment; $comment->setArticle($this); return $this; }