[英]Symfony2 - Passing parameter from the Controller to the FormType
I am using Symfony2. 我正在使用Symfony2。 I want to pass a parameter from the Controller to the FormType : this my Action : 我想将参数从Controller传递给FormType:这是我的Action:
public function addAction(Request $request)
{
$employee = $this->getUser();
if (null === $employee) {
throw $this->createNotFoundException("L'employé est anonyme.");
} else {
$em = $this->getDoctrine()->getManager();
$abs = new Absence();
$form = $this->createForm(new AbsenceType($employee), $abs);
if ($form->handleRequest($request)->isValid()) {
$employee->addAbsence($abs);
$em->persist($employee);
$em->flush();
$this->addFlash('notice', 'absence bien enregistrée.');
return $this->redirect($this->generateUrl('pfe_time_absence_index'));
}
}
return $this->render('PFETimeBundle:Absence:add.html.twig', array(
'form' => $form->createView(),
));
}
This is my FormType : 这是我的FormType:
class AbsenceType extends AbstractType
{
protected $sup;
public function __construct ( Employee $profile)
{
$this->sup = $profile;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$supp=$this->sup;
$builder
->add('employee', 'entity', array(
'class' => 'PFEUserBundle:Employee',
'property'=>'matricule',
'query_builder' => function(EmployeeRepository $er) use ($supp){
return $er->createQueryBuilder('e')
->where("e.parent.matricule = :matricule")
->orderBy('e.nom', 'ASC')
->setParameter('matricule', $supp->getMatricule())
;
}
))
->add('dateDebut','date')
->add('dateFin','date')
->add('motif', 'choice', array(
'expanded' => true,
'multiple' => false,
'choices' => array('Non'=>'Non','Oui'=>'Oui'),))
->add('motif','textarea')
->add('valider','submit')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'PFE\TimeBundle\Entity\Absence'
));
}
public function getName()
{
return 'pfetime_bundle_absence_type';
}
}
the query get all children of the employee and pass them to the entity list( i am using doctrine extension). 该查询获取雇员的所有子代并将其传递到实体列表(我使用的是学义扩展)。 When I execute the action it gives me this error : 当我执行动作时,它给了我这个错误:
Catchable Fatal Error: Argument 1 passed to PFE\TimeBundle\Form\AbsenceType::PFE\TimeBundle\Form\{closure}() must be an instance of PFE\UserBundle\Entity\EmployeeRepository, instance of Gedmo\Tree\Entity\Repository\NestedTreeRepository given, called in C:\wamp\www\Projet\vendor\symfony\symfony\src\Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader.php on line 56 and defined
The actual error you're getting is a problem with the typehint in your query_builder
closure. 您得到的实际错误是query_builder
闭包中的query_builder
问题。
function(EmployeeRepository $er) {}
Your closure is expecting an instance of PFE\\UserBundle\\Entity\\EmployeeRepository
but is receiving an instance of Gedmo\\Tree\\Entity\\Repository\\NestedTreeRepository
您的关闭期望使用PFE\\UserBundle\\Entity\\EmployeeRepository
的实例,但是正在接收Gedmo\\Tree\\Entity\\Repository\\NestedTreeRepository
There are several things that could cause this, but it's likely to be one of these two 可能有几种原因导致这种情况,但很可能是这两种情况之一
Employee
entity doesn't define the repositoryClass
as defined here 您的Employee
实体未定义此处定义的repositoryClass
EmployeeRepository
class doesn't inherit from Gedmo\\Tree\\Entity\\Repository\\NestedTreeRepository
您的EmployeeRepository
类不继承自Gedmo\\Tree\\Entity\\Repository\\NestedTreeRepository
All that being said, you should look into Form Events for features like this. 综上所述,您应该针对此类功能查看表单事件 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.