2
votes

I have two form inside the Controller the first form works fine however my second Form does not work as expected.

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. 
  }
}

My 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

Error : "Expected argument of type "Doctrine\ORM\QueryBuilder", "Doctrine\ORM\Query" given"

Symfony Version : 2.7

1
Remove the getQuery() and maybe change $query to $qb just to make it clear that you are dealing with a query builder object and not a query object. Two very different things. symfony.com/doc/current/reference/forms/types/…. You might get a different error. Not sure what distinct will do to your query. Have to try it and see. - Cerad
Remove the ->getQuery(). Without that you return the query builder, with it you return a query object. Also you can remove the $query = as it's never used so redundant. - qooplmao

1 Answers

4
votes

In query_builder (for building your form) you have to return a QueryBuilder object. Currently, you return a Query object.

Just remove 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
                }