从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');
    },
));

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.