[英]symfony2 select form with Entity Relations
我是symfony2的新手,我必须从3个表/对象中创建一个选择表单:
**Game table:**
id,another_column
game_1, 2
game_2, 4
game_3, 10
game_4, 1
**Score table:**
id,user_id,game_id
1,4,game_1
2,4,game_3
用户通过身份验证后(我正在使用sof用户捆绑包),我必须创建一个包含所有未玩游戏的选择表单。 在这种情况下,我的选择表单需要具有两个选项(game_2和game_4)。
ScoreFormType.php
<?php
/**
* @package evaluation
*/
namespace GameBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ScoreFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('game');
}
public function getName()
{
return 'game_score';
}
}
DefaultController.php
<?php
namespace AppBundle\Controller;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityRepository;
use GameBundle\Entity\Score;
use GameBundle\Form\Type\ScoreFormType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
$m = $this->getDoctrine()->getManager();
$parameters = [];
if (null !== $this->getUser()) {
$score = new Score();
$score
->setUser($this->getUser())
->setPoints(rand(1,50))
;
$form = $this->createForm(new ScoreFormType(), $score);
$parameters['form'] = $form->createView();
}
return $this->render('AppBundle::index.html.twig', $parameters);
}
}
有什么可以帮助我的例子吗? 我试图进行研究,但没有任何意义。
谢谢。
您应该首先从数据库中获取游戏,然后使用以下命令将它们添加到表单中:
$form = $this->createForm(new ScoreFormType(), $games);
在您的ScoreFormType.php中创建以下代码:
public function __construct($aYourGames = array())
{
$this->aYourgames = $aYourGames['Your data'];
}
现在创建您的“选择”:
->add('games', 'choice', array(
'required' => true,
'label' => 'games',
'choices' => $this->aYourgames
)
)
您还可以给您的选择一个类,这将在您使用Javascript时有所帮助:
'attr' => array('class' => 'govChoice'),
我假设以下实体:
游戏 :
class Game
{
private $id;
private $title;
}
得分 :
class Score
{
private $id;
private $user_id;
/**
* @ORM\ManyToOne(targetEntity="Game")
*/
private $game_id;
}
表格类型 :
<?php
/**
* @package evaluation
*/
namespace GameBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ScoreFormType extends AbstractType
{
private $user;
public function __construct($user)
{
$this->user = $user;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$user = $this->user;
$builder->add('game', 'entity', array(
'class' => 'GameBundle:Game',
'property' => 'title',
'query_builder' => function (EntityRepository $er) use ($user) {
return $er->createQueryBuilder('game')
->where('game.id NOT IN(SELECT score.game_id FROM GameBundle:Score score WHERE score.user_id = :user'))
->setParameter('user', $user)
->orderBy('game.title', 'ASC');
));
}
public function getName()
{
return 'game_score';
}
}
DefaultController :
<?php
namespace AppBundle\Controller;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityRepository;
use GameBundle\Entity\Score;
use GameBundle\Form\Type\ScoreFormType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
$m = $this->getDoctrine()->getManager();
$parameters = [];
$form = $this->createForm(new ScoreFormType($this->getUser()));
$parameters['form'] = $form->createView();
return $this->render('AppBundle::index.html.twig', $parameters);
}
}
也许您需要稍微摆弄表单中的查询,但这将是使用表单组件提供的基本工具解决该问题的一种可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.