Major Productions
Major Productions

Reputation: 6042

Symfony2 choice field type - how can I generate a select element from an entity's data?

I want to build a select input element from the following data:

$nameQuery = $em->createQuery('SELECT p.id, p.name FROM MyBundle:Person');
$names = $nameQuery->getResults();

Where each option is:

<select>
    <option value="{{ p.id }}">{{ p.name }}</option>
    .
    .
    .
</select>

I just don't know how to do it with Symfony2's built-in form types. Please assist.

Upvotes: 0

Views: 93

Answers (1)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42799

Use form Builder, In form builder, use this

$builder->add('type', 'entity', array(
    'class' => 'MyBundle:Person',
    'property' => 'name'
));

Upvotes: 2

Related Questions