我是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);
}
}
有没有可以帮助我的例子?我试图做一项研究,但没有任何相关性。
谢谢。
答案 0 :(得分:0)
您应首先从数据库中获取游戏,然后将其添加到表单中:
$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'),
答案 1 :(得分:0)
我假设以下实体:
游戏:
class Game
{
private $id;
private $title;
}
<强>得分强>:
class Score
{
private $id;
private $user_id;
/**
* @ORM\ManyToOne(targetEntity="Game")
*/
private $game_id;
}
<强> FormType 强>:
<?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);
}
}
也许您需要在表单中稍微调整查询,但这可能是使用表单组件提供的基本工具解决它的一种可能性。