symfony2 form querybuilder with parameters

╄→гoц情女王★ 提交于 2019-11-30 08:04:05

You should set the parameter separately like so:

->add( 'weeks', 'entity', array(
    'class' => 'MV\CaravanBundle\Entity\CaravanRow',
    'property' => 'line',
    'query_builder' => function(EntityRepository $er ) use ( $caravan ) {
        return $er->createQueryBuilder('w')
                  ->orderBy('w.dateFrom', 'ASC')
                  ->where('w.caravan = ?1')
                  ->andWhere('w.visible = 1')
                  ->andWhere('w.booked = 0')
                  ->setParameter(1, $caravan);
}

You can either use an integer or string, but the syntax is slightly different for each. See the docs

I recently ran across almost the same problem. Only difference was the 'query_builder' option has to be set inside 'setDefaultOptions'. Basicly the form is created like this:

$builder->add('field', 'query_type', array('id' => 1));

The 'query_type' class looks like this:

class QueryType extends AbstractType
{
     public function setDefaultOptions(OptionsResolverInterface $options)
     {
              $resolver->setRequired(array('id'));

              $resolver->setNormalizers(array(
                  'query_builder' => function (Options $options, $configs) {
                          return function (EntityRepository $er) use ( $options ) {
                              return $er->getSomething( $options['id'] );

                       };
                  },
              ));
     }
}

I use the setNormalizers function to access my $options array and from there on i can call the querybuilder with parameters.

Hope this is useful for someone!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!