【发布时间】:2019-11-19 07:25:00
【问题描述】:
我正在使用 Symfony 框架构建一个表单,并试图了解如何将实体的实例传递给表单构建器。
控制器:
$organization = $user->getOrganization();
$form = $this->createForm(OrganizationCourseType::class, $organization);
OrganizationCourseType 类:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('courses', EntityType::class, [
'class' => Course::class,
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('course')
->andWhere('course.organization = :organization')
->setParameter('organization', $organization);
},
]);
}
但是我得到了错误:
Notice: Undefined variable: organization
如何在表单构建器中访问实体(组织)?我需要将其作为选项传递吗?如果是这样,在控制器的 createForm 调用中包含它有什么意义?
【问题讨论】:
标签: php symfony4 symfony-forms