我是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
在用户通过身份验证后(我使用的是软件用户捆绑包(,我必须创建一个包含所有未播放游戏的选择表单。在这种情况下,我的选择表单需要有两个选项(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';
}
}
默认控制器:
<?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);
}
}
也许您需要稍微处理一下表单中的查询,但这是使用表单组件提供的基本工具解决问题的一种可能性。