Reputation: 147
I am using Symfony2. I want to pass a parameter from the Controller to the FormType : this my 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 :
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
Upvotes: 0
Views: 776
Reputation: 105868
The actual error you're getting is a problem with the typehint in your query_builder
closure.
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
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 hereEmployeeRepository
class doesn't inherit from Gedmo\Tree\Entity\Repository\NestedTreeRepository
All that being said, you should look into Form Events for features like this.
Upvotes: 1