Sam
Sam

Reputation: 779

Symfony2 - Entity form type restriction

I have found something concerning the query_builder, but I have an error:

$builder 
    ->add('title') 
    ->add('comment') 
    ->add('post','entity', array(
        'class' => 'NamespaceXXXXBundle:Post', 
        'query_builder' => function($repository, $id) { 
            return $repository->createQueryBuilder('F') 
                ->where('F.id = :id') 
                ->setParameter('id', $id)
            ; 
        },
    )) 
; 

but I have this error :

Warning: Missing argument 2 for Namespace\XXXXXBundle\Form\{closure} 
(), called in /.../Symfony/Bridge/Doctrine/Form/ChoiceList/ 
EntityChoiceList.php on line 93 and defined in /.../XXXXXBundle/Form/ 
PostType.php line 23 

Please help

Upvotes: 0

Views: 1356

Answers (1)

webda2l
webda2l

Reputation: 6708

Closures work as:

  $id = ##SOMETHING##;

  $builder 
                ->add('title') 
                ->add('comment') 
                ->add('post','entity', array(
                      'class' => 'NamespaceXXXXBundle:Post', 
                      'query_builder' => function($repository) use ($id) { 
                             return $repository->createQueryBuilder('F')
                                               ->where('F.id = :id') 
                                               ->setParameter('id', $id); 
                })) 
    ; 

Upvotes: 2

Related Questions