6

I'm building using the Symfony 3 Forms, and need to retrieve a collection which is dependant on the current user, within the form to render in a dropdown.

Using the EntityType I can retrieve a list of all entities, but I want to be able to run a custom query which only retrieves those which have a relationship with the current user object.

I've read the documentation on forms and the EntityType, and it explains custom queries and mentions passing in a collection as an argument. But I cannot find instructions on how this is achieved anywhere.

Ideally, I'd like to either pass in a collection I've curated in the Controller, pass in the User object to run the query inside the Form, or otherwise access the user id in the form to run the query on.

Has anyone found a solution to anything similar?

2 Answers 2

8

You should try with

pass in the User object to run the query inside the Form

  1. Define required parameter user in options resolver:

    public function configureOptions(OptionsResolver $resolver)
    {
        // ...
        $resolver->setRequired('user');
        // type validation - User instance or int, you can also pick just one.
        $resolver->setAllowedTypes('user', array(User::class, 'int'));
    }
    

It will force you to pass user option so you won't forget about it.

  1. Pass user instance or user ID as an option into the form.

In controller it could look like this:

$this->createForm(SomeFormType::class, $underlyingObjectOrNull, array(
    'user' => $this->getUser(),
));
  1. Build a custom query for EntityType field:

    $user = $options['user'];
    $builder->add('someField', EntityType::class, array(
        'class' => 'AppBundle:SomeEntity',
        'query_builder' => function (EntityRepository $er) use($user) {
            return $er->createQueryBuilder('u')
               //.. -> some method building the query builder
        },
    ));
    

Please note use($user) part which gives you access to this variable inside anonymous function.

0
5

Here is how you can pass custom collection into the form.

For ex: Product entity;

inside controller

$form = $this->createForm(ProductType::class, $product, array('recommended_product' => $recommendedProduct));

inside FormType

 $builder
   ->add('recommended_product', EntityType::class, array(
            'class' => 'AppBundle\Entity\Product',
            'choices' => $options['recommended_product'],
            'choice_label' => function (Product $user) {
                return $user->getProduct();
            }
        ));

 public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Product',
        'recommended_product' => array()
    ));
}
1
  • note: i used Product Entity instead of User as asked by the user
    – Gabf Hann
    Mar 29, 2017 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.