php - Symfony 数组作为表单集合
问题描述
我在 Symfony 中遇到问题好几天了,在谷歌上找不到任何东西。
我有两张桌子。
帐户:
AppBundle\Entity\Account:
type: entity
fields:
name:
type: string
nullable: true
length: 255
oneToMany:
towns:
targetEntity: AppBundle\Entity\Town
mappedBy: owner
cascade: ['persist']
镇:
AppBundle\Entity\Town:
type: entity
fields:
name:
type: string
nullable: true
length: 255
manyToOne:
owner:
targetEntity: AppBundle\Entity\Account
inversedBy: towns
我有一个名称数组:
$names = ['New York','Berlin'];
我想要一个表单,用户可以在其中检查数组中的名称(复选框)。当用户检查“纽约”并提交表单时,我想要一个在字段Town
中带有“纽约”的新实体。name
如果用户取消选中“纽约”,我希望删除该实体。
到目前为止,我尝试使用EntityType
,CollectionType
和ChoiceType
. 我能得到的最好的就是 ChoiceType。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('towns', ChoiceType::class,
[
'choices' =>
[
new Town('New York'),
new Town('Berlin'),
],
'choice_label' => function (Town $town, $key, $index) {
return $town->getName();
},
'choice_value' => function (Town $town) {
return $town->getName();
},
'expanded' => TRUE,
'multiple' => TRUE,
'required' => FALSE,
]
);
}
但是,每次用户提交任何已选中城镇的表单时,它都会添加一个新实体,并且不会删除未选中的城镇......
解决方案
不要使用数组,将城镇存储在数据库中。
然后将towns
字段类型设置为EntityType
。
->add(
'towns',
EntityType::class,
[
'class' => Town::class,
'multiple' => true,
'expanded' => true,
'required' => false,
]
)
将此方法添加到您的 Town 类中,以便在必须将其转换为字符串的任何地方自动显示名称:
/**
* @return string
*/
public function __toString()
{
return $this->name;
}
如果数组的想法是过滤显示的选项以选择城镇,您可以在query_builder
选项中使用它:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$names = ['New York', 'Berlin']; // Or pass it from the controller in $options
$builder
->add('towns',
EntityType::class,
[
'class' => Town::class,
'query_builder' => function (EntityRepository $er) use ($names) {
return $er->createQueryBuilder('town')
->where('town.name IN (:names)')
->setParameter('names', $names);
},
'multiple' => true,
'expanded' => true,
'required' => false,
]
);
}
推荐阅读
- python - 如何在 pandasdmx 中设置属性
- java - 如何在对象之间建立链接/关系?
- angular - 类型可观察
不可分配给 Observable 类型 - kubernetes - 在 SharedVPC 上访问 GKE 内部 LB IP 超时
- javascript - HTML 和 CSS 弹出卡
- c++ - NFA 程序接受至少有一个字符出现在 3 的倍数中的字符串
- html - Iissue with formsubmit.co url
- java - Junit 测试(使用 when 和 Mockito verifyNoMoreInteractions)
- arrays - 将对象数组转换为对象分组数组 Typescript
- node.js - 为什么我的 package-lock.json 与 package.json 不同步?