Symfony表单错误:"类型" Doctrine \ ORM \ QueryBuilder"," Doctrine \ ORM \ Query"的预期参数给定"

时间:2016-02-22 15:06:49

标签: php symfony orm doctrine-orm

我在Controller中有两个表单,第一个表单工作正常但是我的第二个表单没有按预期工作。

myController的:

// Second Form
$formTwo = $this->get('form.factory')->createNamedBuilder('form2name', new CarType(), null, array())
        ->getForm();

if('POST' === $request->getMethod()) {

if ($request->request->has('form1name')) {
    // handle the first form  
}

if ($request->request->has('form2name')) {
    // handle the second form 
    // get the id value of the selected value. 
  }
}

我的CarType:

public function buildForm(FormBuilderInterface $builder, array $options){

   $builder->add('makename','entity',array(
                    'class'=> 'MyTestBundle:Car\CarModel',
                    'query_builder'=>function(EntityRepository $er){
                        return $query = $er->createQueryBuilder('s')
                                    ->select('s.makename')
                                    ->distinct()
                                    ->orderBy('s.makename','ASC');
                }

   ));
  $builder->add('search','submit',array());
}

My Car Entity

Full Stack Trace

  

错误:"类型" Doctrine \ ORM \ QueryBuilder"," Doctrine \ ORM \ Query"的预期参数给定"

Symfony版本:2.7

1 个答案:

答案 0 :(得分:2)

query_builder(用于构建表单)中,您必须返回QueryBuilder个对象。目前,您返回Query个对象。

Juste删除getQuery()

 $builder->add('makename','entity',array(
                    'class'=> 'MyTestBundle:Car\CarModel',
                    'query_builder'=>function(EntityRepository $er){
                        return $er->createQueryBuilder('s')
                                    ->select('s.makename')
                                    ->distinct()
                                    //->getQuery(); remove this line
                }