コード例 #1
0
 /**
  * Chức năng: Tạo ra một bài viết mới
  * @Route("/user/new", name = "new_article")
  */
 public function newAction(Request $request)
 {
     $user = new User();
     $user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findOneById(1);
     $article = new Article();
     $article->setPublishedAt(new \DateTime('today'));
     $article->setNumView(0);
     $article->setNumLike(0);
     $article->setNumUnlike(0);
     $article->setNumShare(0);
     $article->setUser($user);
     $form = $this->createFormBuilder($article)->add('title', 'text')->add('summary', 'textarea')->add('content', 'textarea')->add('publishedAt', 'date')->add('tag', 'text')->add('specialTag', 'text')->add('create', 'submit', array('label' => 'Create News'))->getForm();
     $form->handleRequest($request);
     if ($form->isValid()) {
         $entityManager = $this->getDoctrine()->getManager();
         $entityManager->persist($article);
         $entityManager->flush();
         return $this->redirectToRoute('show_article');
     }
     return $this->render('default/new.html.twig', array('form' => $form->createView()));
 }