Symfony 2 : Access database inside FormBuilder Symfony 2 : Access database inside FormBuilder symfony symfony

Symfony 2 : Access database inside FormBuilder


Use type entity instead of choice

$builder  ->add('entity_property', 'entity', array(    'class' => 'Namespace\\To\\Entity',    'query_builder' => function(EntityRepository $repository) {       return $repository->createQueryBuilder('q')          ->where('q.a_field = yourvalue');     }));

Edit:

Two ways for using custom parameters in your query. In both situations, the parameters are injected from outside, so your FormType don't need any references to the session or request objects or whatever.

1- Pass required parameters to your constructor

class TaskType extends AbstractType{    private $custom_value;    public function __construct($custom_value) {        $this->custom_value = $custom_value;    }    // ...}

in your buildForm() you must copy the value to local variable and make it available for the query_builder callback:

public function buildForm(/*...*/) {   $my_custom_value = $this->custom_value;   // ...       'query_builder' => function(EntityRepository $repository) use ($my_custom_value) {           return $repository->createQueryBuilder('q')                ->where('q.a_field = :my_custom_value')               ->setParameter('my_custom_value', $my_custom_value);       }   // ...}

2- use the $options parameter of the buildForm method.

First you have to define a default value by overriding getDefaultOptions:

public function getDefaultOptions(array $options){    return array(        'my_custom_value' => 'defaultvalue'    );}

Then you can pass it from your controller in the third argument of the createForm method.

$this->createForm(new YourFormType(), $entity, array('my_custom_value' => 'custom_value'));

Now the value should be available through the $options parameter of youru buildForm method. Pass it to the callback as described above.


In Symfony 2.1

You now have to use the OptionsResolverInterface within the setDefaultOptions method. Here is the code you would have to use if you wanted to retrieve the options (using the same example as the accepted answer)


use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\OptionsResolver\OptionsResolverInterface;public function buildForm(FormBuilderInterface $builder, array $options){    parent::buildForm($builder, $options);   $my_custom_value = $options[custom_value];   // ...       'query_builder' => function(EntityRepository $repository) use ($my_custom_value) {           return $repository->createQueryBuilder('q')                ->where('q.a_field = :my_custom_value')               ->setParameter('my_custom_value', $my_custom_value);       }   // ...}    public function setDefaultOptions(OptionsResolverInterface $resolver)    {        $resolver->setDefaults(array(            'my_custom_value' => 'defaultvalue'        ));    }

You still pass the options in the same way:

$this->createForm(new YourFormType(), $entity, array('my_custom_value' => 'custom_value'));