Can i use query in form builder to get filtered collection in symfony form

前端 未结 5 780
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 16:42

IN the AcmePizza BUndle this is working fine

->add(\'pizza\', \'entity\', array(
                \'class\'         => \'Acme\\PizzaBundle\\Entity\\Pizza\',         


        
5条回答
  •  春和景丽
    2021-02-05 17:19

    In Symfony 2.7 i solved this by doing the following in the UserTaskType:

    add('the_name', 'entity', array(
                    'class' => 'acme\myBundle\Entity\UserTask',
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('u')
                            ->where('u.id > :id')
                            ->setParameter('id', '1')
                            ->orderBy('u.username', 'ASC');
                    },
                ));
        }
    
        /**
         * @param OptionsResolver $resolver
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'acme\myBundle\Entity\UserTask',
            ));
        }
    
    }
    

提交回复
热议问题