Mirage
Mirage

Reputation: 31548

How to use Doctrine Create query in form type class

If I use this code then I can create new object without any problem:

 ->add('user', 'entity', array(
                 'class' => 'Acme\Entity\User',
                 'query_builder' => function(EntityRepository $er) use ($options){
                    return $er->createQueryBuilder('u')->orderBy('u.name', 'ASC');}
                ))

But if I set some parameter like this:

'query_builder' => function(EntityRepository $er) use ($options) {
                            return $er->createQueryBuilder('u') 
->where('u.id = :id')
->setParameter('id',$options['id'])
->orderBy('u.name', 'ASC');}
                ))

Then I can see the options correctly in select box , but when i submit the form , i get NULL values for User Object. Am missing something?

Upvotes: 0

Views: 414

Answers (1)

Mick
Mick

Reputation: 31919

$options['id'] might be a string. Try this:

->setParameter('id',intval($options['id']))

Upvotes: 1

Related Questions