symfony2 pass parameter to query_builder in form type


   public function buildForm(FormBuilderInterface $builder, array $options)
    {
     $builder ->add('cityName','entity',array(
           'class' => 'ModerationBundle:TVille',
                         'query_builder' => function(EntityRepository $er) use ($options) {
                        return $er->createQueryBuilder('u')
                        ->where('u.section = :id')
                        ->setParameter('id', $options['id'])
                        ->orderBy('u.root', 'ASC')
                        ->addOrderBy('u.lft', 'ASC');
                },);
In this way we can pass the parameter to query builder. and the options sould passed by this way:
$form = $this->createForm(new ProductType(), $product, array('id' => $id ));

评论

热门博文