2016-02-29 90 views
0

我有兩個表:會話和遊戲(一個會話可以有多個遊戲)Symfony2的形式實體一對多隻有一個實體不起作用

後,我添加它使我的遊戲形式的會議。但現在我有一個奇怪的行爲: 當我拿着一個EntityType表單加載我的所有會話時,遊戲被正確保存在我的數據庫中。但是,當我只在窗體中加載一個會話時,數據庫中的session_id字段爲NULL。

遊戲實體:

/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @ORM\ManyToOne(targetEntity="Session", inversedBy="game") 
* @ORM\JoinColumn(name="session_id") 
*/ 
private $session; 

會話實體:

/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @ORM\OneToMany(targetEntity="Game", mappedBy="session") 
*/ 
private $game; 

控制器:

/** 
* @param Request $request 
* @return \Symfony\Component\HttpFoundation\Response 
* @Route("/session/new") 
*/ 
public function addnewsessionAction(Request $request) 
{ 
    $session = new Session(); 

    $form = $this->createForm(SessionType::class, $session); 
    $form->handleRequest($request); 

    if ($form->isSubmitted()) 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($session); 
     $em->flush(); 

     return $this->redirectToRoute('app_game_addnewsessiongame', array('sessionid' => $session->getId())); 
    } 

    return $this->render(':session:addsession.html.twig', array(
     'form' => $form->createView(), 
    )); 
} 


/** 
* @param Request $request 
* @return \Symfony\Component\HttpFoundation\Response 
* @Route("/session/{sessionid}/addnewgame") 
*/ 
public function addnewsessiongameAction(Request $request, $sessionid) 
{ 
    $game = new Game(); 

    $form = $this->createForm(GameType::class, $game, array('sessionid' => $sessionid)); 
    $form->handleRequest($request); 

    if ($form->isSubmitted()) 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($game); 
     $em->flush(); 

     return $this->redirectToRoute('app_game_addnewsessiongame', array('sessionid' => $sessionid)); 
    } 

    return $this->render(':game:addgame.html.twig', array(
     'form' => $form->createView(), 
     'sessionid' => $sessionid, 
    )); 
} 

遊戲類型(遊戲正確插入時,我手動選擇會話):

$builder 
     ->add('session', 'entity', array(
      'class' => 'AppBundle\Entity\Session', 
      'query_builder' => function(EntityRepository $er) use ($options) { 
       return $er->createQueryBuilder('w'); 
      }, 
     )) 

遊戲類型(當只有一個會話中給出槽的控制器):

 $builder 
     ->add('session', 'entity', array(
      'class' => 'AppBundle\Entity\Session', 
      'query_builder' => function(EntityRepository $er) use ($options) { 
       return $er->createQueryBuilder('w') 
        ->where('w.id = ?1') 
        ->setParameter(1, $options['sessionid']); 
      }, 

在情況下,兩個選擇選項僅顯示所述給定實體,而是被保存在與NULL分貝。即使我再次選擇這個選項,NULL也會保存在db中。

我想自動保存正確的會話,所以用戶不必選擇會話。將顯示選擇表單:隱藏了額外的'attr'=>數組('class'=>'hidden')

回答

0

經過一晚的睡眠後,我得到了解決方案。我把會話實體,並把它槽的形式進入遊戲實體

控制器:

/** 
* @param Request $request 
* @return \Symfony\Component\HttpFoundation\Response 
* @Route("/session/{sessionid}/addnewgame") 
*/ 
public function addnewsessiongameAction(Request $request, $sessionid) 
{ 
    $game = new Game(); 

    $em = $this->getDoctrine()->getManager(); 
    $mysession = $em->getRepository('AppBundle:Session')->find($sessionid); 

    $game->setSession($mysession); 

    $form = $this->createForm(GameType::class, $game, array('sessionid' => $sessionid)); 
    $form->handleRequest($request); 

    if ($form->isSubmitted()) 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($game); 
     $em->flush(); 

     return $this->redirectToRoute('app_game_addnewsessiongame', array('sessionid' => $sessionid)); 
    } 

    return $this->render(':game:addgame.html.twig', array(
     'form' => $form->createView(), 
     'sessionid' => $sessionid, 
    )); 
} 

遊戲類型:

$builder->add('session','entity', array('class' => 'AppBundle\Entity\Session','attr' => array('class' => 'hidden')))