简体   繁体   English

Symfony 3:如何在包含大量对象的 Form 中实现 EntityType?

[英]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对象,其中ManyToOneCity有关系。 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 :有了这个步骤:

  1. Create a custom form type inheriting from a simple TextType创建一个继承自简单 TextType 的自定义表单类型
  2. Use this custom form type with a javascript autocomplete plugin and an ajax call将此自定义表单类型与 javascript 自动完成插件和 ajax 调用结合使用
  3. Create your custom action in order to retrieve your choices for your select box (created by the plugin)创建您的自定义操作以检索您对选择框的选择(由插件创建)
  4. Use a Data Transformer in your custom form type in order to retrieve objects instead of values在自定义表单类型中使用数据转换器以检索对象而不是值

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM