使用 Symfony 访问传递给 Form Builder 的实体
Accessing Entity Passed to Form Builder with Symfony
我正在使用 Symfony 框架构建表单,并试图了解如何将实体实例传递给表单构建器。
控制器:
$organization = $user->getOrganization();
$form = $this->createForm(OrganizationCourseType::class, $organization);
组织课程类型class:
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 调用中有什么意义?
在您的控制器中,将您的实体实例作为第三个参数传递给您的 $form
定义行:
$form = $this->createForm(OrganizationCourseType::class, null, ['organization' => $organization]);
然后像这样在您的 FormType class 中检索它:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$organization = $options['organization'];
$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);
},
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(['organization']);
}
解决问题的第二部分:"what is the point of including it in the createForm
call ?"
当您将对象作为第二个参数传递给 createFormBuilder
时,您传递的是表单的初始数据。 Symfony 将尝试在对象中查找与表单字段名称匹配的属性(或 getters/setters)并将其值分配给该字段。然后,在提交时,它会相应地更新您的模型。
您通常会传递与表单 data_class
相同类型的对象,因此在您的情况下,这将是 OrganizationCourse
。您可以执行以下操作:
$organizationCourse = new OrganizationCourse();
$organizationCourse->setOrganization($user->getOrganization());
$form = $this->createForm(OrganizationCourseType::class, $organizationCourse);
您可以 select 许多 Courses
并将它们分配给 Organization
。
但是,这看起来不像您的用例,因为 OrganizationCourse
看起来像关系而不是实体,因此请参考 .
我正在使用 Symfony 框架构建表单,并试图了解如何将实体实例传递给表单构建器。
控制器:
$organization = $user->getOrganization();
$form = $this->createForm(OrganizationCourseType::class, $organization);
组织课程类型class:
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 调用中有什么意义?
在您的控制器中,将您的实体实例作为第三个参数传递给您的 $form
定义行:
$form = $this->createForm(OrganizationCourseType::class, null, ['organization' => $organization]);
然后像这样在您的 FormType class 中检索它:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$organization = $options['organization'];
$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);
},
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(['organization']);
}
解决问题的第二部分:"what is the point of including it in the createForm
call ?"
当您将对象作为第二个参数传递给 createFormBuilder
时,您传递的是表单的初始数据。 Symfony 将尝试在对象中查找与表单字段名称匹配的属性(或 getters/setters)并将其值分配给该字段。然后,在提交时,它会相应地更新您的模型。
您通常会传递与表单 data_class
相同类型的对象,因此在您的情况下,这将是 OrganizationCourse
。您可以执行以下操作:
$organizationCourse = new OrganizationCourse();
$organizationCourse->setOrganization($user->getOrganization());
$form = $this->createForm(OrganizationCourseType::class, $organizationCourse);
您可以 select 许多 Courses
并将它们分配给 Organization
。
但是,这看起来不像您的用例,因为 OrganizationCourse
看起来像关系而不是实体,因此请参考