我正在使用表单生成器来创建用于应用程序的表单。在表单中,用户需要能够选择与其关联的事件列表。但是,我无法弄清楚如何将用户 ID 传递给表单生成器?
我的代码现在是这样的:
EvType.php
<?php
// src/Acme/MembersBundle/Form/Type/EvType.php
// This is to handle forms for the Members Form
namespace Acme\MembersBundle\Form;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class EvType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$centre = $this->centre;
$builder->add('id', 'integer', array('required'=>false));
$builder->add('list','entity', array('class'=>'Acme\InstructorBundle\Entity\MapLists', 'property'=>'name',
'query_builder' => function(EntityRepository $br) {
return $br->createQueryBuilder('ml')
->where('ml.user = :user')
->setParameter('user','1' );
}));
$builder->add('eventHorse', 'text', array('required'=>false));
}
public function getName()
{
return 'ev';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\InstructorBundle\Entity\MapListCentreMembers',
'csrf_protection' => false,
'csrf_field_name' => '_token',
// a unique key to help generate the secret token
'intention' => 'task_item',
));
}
}
->setParameter('user','1' );
是我希望能够从表单中传递用户 ID 的地方。现在,我已经静态分配了用户 ID。
DefaultController.php
// User ID
$userid = $mem['userID'];
// Get Tests from Entity for Form use
$memberEV = $dm->getRepository('InstructorBundle:MapListMembers')->find($memberint);
// Generate Form to edit Tests & Achievements
$ev = $this->createForm( new EvType(), $memberEV);