duanou1904 2013-10-21 14:54
浏览 45

从EntityType查询数据

I am using Form/Type FormBuilder to set up a Form. But how can I get data from Repository into the Form Type? The following code bellow does not work.

Using a Custom Query for the Entities

If you need to specify a custom query to use when fetching the entities (e.g. you only want to return some entities, or need to order them), use the query_builder option. The easiest way to use the option is as follows:


use Doctrine\ORM\EntityRepository;
// ...

$builder->add('users', 'entity', array(
    'class' => 'AcmeHelloBundle:User',
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('u')
            ->orderBy('u.username', 'ASC');
    },
));
  • 写回答

1条回答 默认 最新

  • duanfan9859 2013-10-21 15:08
    关注

    You need to specify which entity field to use as a label for each option using property attribute - http://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage

    -- OR ---

    You may omit property attribute and add __toString() magic method to your entity. This way you will have more control of what will be used as options labels.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)