コード例 #1
0
 /**
  * @param Request $request
  * @Route("/game/new", name="gameNew")
  * @Method({"GET", "POST"})
  * @Template("AppBundle:admin/form:game.html.twig")
  *
  * @link http://symfony.com/doc/2.8/cookbook/form/form_collections.html
  *
  * @return Response
  */
 public function newGameAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $game = new Game();
     $game->setGamedate(new \DateTime('now'));
     $score['host'] = new GameScore();
     $score['guest'] = new GameScore();
     $game->addScore($score['host']->setSide('host'));
     $game->addScore($score['guest']->setSide('guest'));
     $form = $this->createForm(GameType::class, $game, ['em' => $em, 'action' => $this->generateUrl('gameNew'), 'method' => Request::METHOD_POST])->add('save', SubmitType::class, array('label' => 'Save'));
     if ($request->getMethod() == 'POST') {
         $form->handleRequest($request);
         if ($form->isValid()) {
             $em->persist($game);
             $em->flush();
             return $this->redirectToRoute('adminGames');
         }
     }
     return ['form' => $form->createView()];
 }