I'm using Symfony 2.7 with Doctrine. My controller actions often look like:
# my/namespace/Controller/ItemsController.php -> listAction()
$items = $this->get('repository.items')->findAll();
return $this->render('itemsList.html.twig', array('items' => $items));
In my templates I like to iterate associated Entities:
# my/namespace/Resources/views/itemsList.html.twig
{% for item in items %}
Item: {{ item.name }} <br/>
Groups: <br/>
<ul>
{% for group in item.groups %}
<li>{{ group.name }}</li>
{% endfor %}
</ul>
{% endfor %}
This causes many SELECT-queries, which I'd like to avoid. The only solution I know so far is collecting all needed data in a repository
and assigning it in the action
, instead of crawling within the template.
I'd like to keep it that way as seen in the twig-snippet. Are there any smart caching options that might detect cases like mine and optimize the queries automatically?
Thanks in advance!