douzuizhuo0587 2017-10-25 14:24 采纳率: 0%
浏览 10
已采纳

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

I have an entity City with more than 30000 objects stored. User is able to add an Address object with a relation ManyToOne to City. But during Form building, the rendering of <input> type radio or select is not appropriate for the number of objects...

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. But I haven't idea on how implement this.

Do you have a solution on my issue please?

Thank you

  • 写回答

2条回答 默认 最新

  • dqsvnsad79721 2017-10-25 14:28
    关注

    With this steps :

    1. Create a custom form type inheriting from a simple TextType
    2. Use this custom form type with a javascript autocomplete plugin and an ajax call
    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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line