public function load(ObjectManager $manager) { for ($i = 0; $i < 40; $i++) { $article = new Article(); $article->setTitle('News' . $i); $article->setContent('This is default content'); $article->setDatetime(new \DateTime('- ' . rand(1, 4) . ' day')); $commentA = new Comment(); $commentA->setAuthor('Author'); $commentA->setContent('Comment .... default'); $manager->persist($commentA); $article->addComment($commentA); $commentB = new Comment(); $commentB->setAuthor('Author' . $i); $commentB->setContent('Comment .... default'); $manager->persist($commentB); $article->addComment($commentB); $manager->persist($article); } $manager->flush(); }
/** * @Route("/articles/{slug}") * @Template("AppBundle:Article:show.html.twig") * @ParamConverter("article", options={"mapping": {"slug": "slug"}, "entity_manager" = "default"}) */ public function showAction(Article $article, Request $request) { // @TODO: move this in service/event $em = $this->getDoctrine()->getManager(); $em->persist(new Reading($this->getUser(), $article)); $em->flush(); // Comment to highlight $highlight = null; $comment = new Comment(); $form = $this->createForm(new CommentType(), $comment); $form->handleRequest($request); if ($form->isValid()) { $article->addComment($comment); $comment->setAuthor($this->getUser()); $em->persist($comment); $em->flush(); // Remove field after persist $form = $this->createForm(new CommentType(), new Comment()); $highlight = $comment; } return array('article' => $article, 'form' => $form->createView(), 'highlight' => $highlight); }