只有一个实体的Symfony2表单实体OneToMany不起作用

时间:2016-02-29 19:17:03

标签: php symfony doctrine

我有两张桌子:会话和游戏(一个会话可以有多个游戏)

添加会话后,它会渲染我的游戏表单。但现在我有一个奇怪的行为: 当我使用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,
    ));
}

GameType(手动选择会话时正确插入游戏):

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

GameType(当通过控制器只给出一个会话时):

        $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']);
            },

在第二种情况下,select选项仅显示给定实体,但在db中保存为NULL。即使我再次只选择一个选项,NULL也会保存在db。

我想自动保存正确的会话,因此用户无需选择会话。将显示选择表单:隐藏其他'attr'=>数组('class'=>'hidden')

1 个答案:

答案 0 :(得分: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')))