Symfony2 - 实体表单类型限制

时间:2011-12-13 15:07:58

标签: symfony

我找到了一些关于query_builder的内容,但我有一个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)
            ; 
        },
    )) 
; 

但我有这个错误:

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 

请帮忙

1 个答案:

答案 0 :(得分:2)

闭包的工作方式如下:

  $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); 
                })) 
    ;