我有一个实体 City
存储元素超过30000件。用户可以添加Address
具有关系的对象 ManyToOne
至City
。
但是在Form构建过程中,渲染了<input>
输入 radio 或 select
不适合对象的数量...
我使用以下实现代码(它是一个片段):
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* Pattern to get cities */
$pattern = 'Bor%'; //I use this filter to reduce the number of objects but not sufficient
$builder
->add('city', EntityType::class, array(
'class' => 'AppPlatformBundle:City',
'choice_label' => 'name',
'multiple' => false,
'expanded' => true,
'query_builder'=> function(CityRepository $repository) use($pattern) {
return $repository->getCitiesWithPattern($pattern);
}));
}
我认为解决方案是使用 TextType,其中用户可以在开始输入任何内容时选择建议。但我不知道如何实现这一点。
请问您对我的问题有解决办法吗?
谢谢
最佳答案
按照以下步骤:
- 创建继承自简单 TextType 的自定义表单类型
- 将此自定义表单类型与 JavaScript 自动完成插件和 ajax 调用结合使用
- 创建自定义操作,以便检索您对选择框(由插件创建)的选择
- 在自定义表单类型中使用数据转换器来检索对象而不是值
关于php - 交响乐团 3 : How can I implement an EntityType in Form with a lot of objects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46934978/