我正在使用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');
},
));
答案 0 :(得分:0)
您需要使用property
属性指定要用作每个选项的标签的实体字段 - http://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage
- 或---
您可以省略property
属性并向您的实体添加__toString()
魔术方法。这样您就可以更好地控制将用作选项标签的内容。