Q
Q
Qixing2015-02-02 10:26:40
symfony
Qixing, 2015-02-02 10:26:40

How to exclude query_builder in Symfony2 buildForm?

$builder
            ->add(
                'maker',
                'entity',
                array(
                    'class' => 'CrbrBundle:CrbrItemMaker',
                    'property' => 'name',
                    'required' => false,
                    'data' => $options['maker'],
                    'empty_value' => '-- Все --',
                    'query_builder' =>  function (EntityRepository $er) use ($options) {

                            return $er->createQueryBuilder('x')
                                ->innerJoin('x.item', 'i')
                                ->innerJoin('i.category', 'c')
                                ->innerJoin('c.parentCategory', 'h')
                                ->andWhere('h.categoryGroup = :groupCategory')
                                ->setParameter(
                                    'groupCategory',
                                    $options['groupCategory']
                                );
                        } 
                ,
                )
            )

Hello. I want, under a certain condition $options['groupCategory'] == 3, so that the 'query_builder' option is not executed. There just wasn't such a thing.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
keltanas, 2015-02-03
@Qixing

Make the form without the `marker` field, and add it on the FormEvents::PRE_SET_DATA event when the necessary conditions match.
See symfony.com/doc/current/cookbook/form/dynamic_form...

B
Boris Benkovsky, 2015-02-02
@benbor

if .... else doesn't work anymore?

R
romashka_sky, 2015-02-04
@romashka_sky

'query_builder' =>  function (EntityRepository $er) use ($options) {
                            if ($options['groupCategory'] == 3) {
                                 return $er->createQueryBuilder('x');
                            }
                            return $er->createQueryBuilder('x')
                                ->innerJoin('x.item', 'i')
                                ->innerJoin('i.category', 'c')
                                ->innerJoin('c.parentCategory', 'h')
                                ->andWhere('h.categoryGroup = :groupCategory')
                                ->setParameter(
                                    'groupCategory',
                                    $options['groupCategory']
                                );
                        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question