Symfony 2 - how to pass data to formBuilder?

前端 未结 6 601
独厮守ぢ
独厮守ぢ 2020-11-30 03:50

I\'m using entity choice list in my form. I want to use only specific entities (in example: only groups that user belongs to) So, in controller, I\'m getting these groups, a

6条回答
  •  执念已碎
    2020-11-30 04:14

    If you want to use custom query, you have to set query_builder option as follows:

    use Doctrine\ORM\EntityRepository;
    
    ...
    
    $message = new Message();
    
    $form = $this->createFormBuilder($message)
                 ->add('group', 'entity', array(
                       'class' => 'Vendor\MyBundle\Entity\Group',
                       'label'=>'Group:',
                       'query_builder' => function(EntityRepository $er) {
                           return $er->createQueryBuilder('g')
                                     ->... // whatever you want to do
                           }
                        ))
                 ->getForm();
    

    You can find more info about query builder in Doctrine manual and about options for entity in Symfony2 manual.

提交回复
热议问题