Symfony2 - How to set, and get, options when using a Form Class?

后端 未结 6 1530
暖寄归人
暖寄归人 2021-02-06 08:29

I am using Form Classes to build the various form in my project.

In the Entity Type file, for the buildForm function, there is a secondary parameter of \"array $options\

6条回答
  •  無奈伤痛
    2021-02-06 09:06

    Well, it turns out, Gregoires answer was very close, but gave me and "Undefined variable" error when trying to actually but the variable into the createQueryBuilder function.

    I spent a while trying to figure out why and found the issue. You have to add an extra parameter to the function in the "query_builder" option, like such:

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

    The magic setting being "use ($options)". This allows you to successfully use $options['id'] in the Query Builder.

提交回复
热议问题