2

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

2
  • 2
    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. Commented Feb 22, 2016 at 15:12
  • 2
    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. Commented Feb 22, 2016 at 15:12

1 Answer 1

5

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
                }
Sign up to request clarification or add additional context in comments.

6 Comments

Also the assignation to the $query variable
I get a different Error now : Warning: spl_object_hash() expects parameter 1 to be object, string given
Ok, could you update your post and copy the full trace in order to know exactly when this error is thrown and what is this string, please ?
yes because i want to create dynamic dropdown and "makename" is the first dropdown option and I think now it works... I removed "select" and added "choice_label" $builder->add('makename', 'entity', array( 'class' => 'MyTestBundle:Car\CarModel', 'choice_label'=>'makename', 'query_builder' => function (EntityRepository $er) { return $qb = $er->createQueryBuilder('s') ->GroupBy('s.makename') ->orderBy('s.makename','ASC'); } ));
And I also had to change from distinct() to GroupBy() and got it working... Thanks :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.