[英]Symfony 3: How can I implement an EntityType in Form with a lot of objects?
I have an entity City
with more than 30000 objects stored.我有一个实体City
其中存储了 30000 多个对象。 User is able to add an Address
object with a relation ManyToOne
to City
.用户可以添加一个Address
对象,其中ManyToOne
与City
有关系。 But during Form building, the rendering of <input>
type radio or select
is not appropriate for the number of objects...但是在Form构建过程中, <input>
类型radio或者select
的渲染对于对象的数量来说是不合适的...
I use the following implementation code (it is a snippet):我使用以下实现代码(它是一个片段):
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);
}));
}
I think that a solution is to use a TextType where the proposals can be selected by the user when he start type anything.我认为一个解决方案是使用 TextType,当用户开始输入任何内容时,用户可以选择建议。 But I haven't idea on how implement this.但我不知道如何实现这一点。
Do you have a solution on my issue please?请问您对我的问题有解决方案吗?
Thank you谢谢
With this steps :有了这个步骤:
Thank you so much Fabien.非常感谢法比安。
I was able to implement a good solution for me :)我能够为我实现一个很好的解决方案:)
So for anyone interrested by my solution:所以对于任何对我的解决方案感兴趣的人:
I define city field as HiddenField like:我将城市字段定义为 HiddenField,例如:
$builder
->add('city', HiddenType::class);
I use a Data Transformer to convert City
object into unique Id (field Id of the object)我使用数据转换器将City
对象转换为唯一的 Id(对象的字段 Id)
In Twig template, I implement jQuery-ui Autocomplete在 Twig 模板中,我实现了jQuery-ui Autocomplete
In backend, I create a route able to return a Json response with all cities filtered by the term
of jQuery plugin.在后端,我创建了一个能够返回 Json 响应的路由,其中所有城市都由 jQuery 插件的term
过滤。
In frontend, I develop a jquery script that append a new field "city" with type Text
and autocomplete action.在前端,我开发了一个 jquery 脚本,它附加了一个新的字段“城市”,类型为Text
和自动完成操作。
When response is received, I fill the field city
with type Hidden
with the id.当收到响应时,我用 id 填充类型为Hidden
的字段city
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.