我有3个实体。 “挑战”/“团队”/“用户” 我正在尝试向嵌入式表单添加值,如下所示:
class ChallengeController extends Controller
{
public function newAction($id, Request $request)
{
// 1. Get known users
$user = $this->container->get('security.context')->getToken()->getUser();
$challengedUser = $this->getDoctrine()->getRepository('ApplicationSonataUserBundle:User')->find($id);
// 2. Create new team
$team = new Team();
$teamOpponent = new Team();
// 3. Add known players to corresponding teams
$team->addUser($user);
$teamOpponent->addUser($challengedUser);
// 4. Create new challenge and add teams
$challenge = new Challenge();
$challenge->addTeam($team);
$challenge->addTeam($teamOpponent);
// 5. Create new form
$form = $this->createForm(new ChallengeType(), $challenge);
return $this->render('TennisconnectDashboardBundle:Challenge:new.html.twig', array('form' => $form->createView(), 'challengedUser' => $challengedUser));
}
}
//编辑:添加了challengeType
class ChallengeType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('teams', 'collection', array(
'type' => new TeamType(),
'allow_add' => true
))
->add('place')
->add('date');
}
public function getName()
{
return 'challenge';
}
public function getDefaultOptions(array $options)
{
return array('data_class' => 'Tennisconnect\DashboardBundle\Entity\Challenge');
}
}
当提出挑战表格时,我会期望填写2名球员的价值。但他们只是空白。我做错了什么?
THX。