Reputation: 2873
I've been looking at various similiar questions and answers, but none fits this or solves my problem. I feel I'm missing something obvious because this seems to crazy simple and common. Anyways:
I want to build a table view from which the user can select entities for further processing. Obviously, I need to display more than just a name, I want to add a multitude of data.
The 'entity' form type seems the natural choice, so I've defined a form type including this:
$builder->add('items', 'entity', array(
'multiple'=>true,
'expanded'=>true,
'class'=>'MySiteBundle:Item', 'property'=>'name', 'query_builder'=>function(EntityRepository $er) use ($catagory) {
return $er->createQueryBuilder('s')->where('s.category = :cat')->andWhere('s.available=true')->orderBy('s.name')->setParameters(array('cat'=>$category));
}));
Thinking this would somehow give me access to the entity in twig. But no matter what I try, via form.items.vars or .get("value") or two dozen other things suggested to similar questions, nothing works.
All I want is a table with item data (name, price, etc. etc.) and I can't believe this is so difficult. What easy, obvious thing am I missing?
I need to use a form because there is other data the user needs to enter and I want to make use of validation.
Upvotes: 3
Views: 6168
Reputation: 44841
You can't get the access to the entities behind the entity
type from a form view. Because of that, you need to fetch the items in a separate query. To make this easy, make the keys of the array of items correspond to their IDs. Provided you send the items
array to the template, you could output it like this:
<table>
{% for item in form.items %}
{% set id = item.get('value') %}
<tr>
<td>{{ form_widget(item) }}</td>
<td>{{ items[id].name }}</td>
<td>{{ items[id].category.name }}</td>
<td>{{ items[id].available ? 'Yes' : 'No' }}</td>
{# and so on ... #}
</tr>
{% endfor %}
</table>
Upvotes: 4