从实体类型查询数据


Query data from EntityType

我正在使用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');
    },
));
您需要

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

--或---

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