I am using Form Classes to build the various form in my project.
In the Entity Type file, for the buildForm function, there is a secondary parameter of \"array $options\
Let me show you what worked for me
In the Controller:
$form = $this->createForm(new UsersType(), $entity, array(
'attr' => array('locationId' => $currentLocationId)));
In the FormType:
->add('location', 'entity', array(
'class' => 'Ro\RoinventBundle\Entity\Locations',
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($options)
{
if (isset($options['attr']['locationId']) && ($options['attr']['locationId'] != NULL))
{
return $er->createQueryBuilder('Locations')
->where('Locations.id = :param')
->setParameter('param', $options['attr']['locationId']);
}
//else do what you want
},
));