从EntityType查询数据

时间:2013-10-21 14:54:55

标签: php symfony doctrine-orm

我正在使用Form / Type FormBuilder来设置表单。但是如何从存储库中获取数据到表单类型?以下代码不起作用。

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 个答案:

答案 0 :(得分:0)

您需要使用property属性指定要用作每个选项的标签的实体字段 - http://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage

- 或---

您可以省略property属性并向您的实体添加__toString()魔术方法。这样您就可以更好地控制将用作选项标签的内容。

相关问题