Due to the answer of my last question I was able to reduce the SELECT
-queries on the first level. Unfortunately the associated Entities are linked deeper, e.g.:
Item -> Group -> Subscriber -> User -> username
Repository method:
// ItemRepository
public function findAll() {
return $this->createQueryBuilder('item')
->addSelect('groups')->join('item.groups', 'groups')
->getQuery()->getResult()
}
twig template:
{% for item in items %}
{# Level: 0 #}
Name: {{ item.name }}<br/>
Groups:<br/>
<ul>
{# Level: 1 #}
{% for group in item.groups %}
<li>{{ group.name }}<br/>
<ol>
{# Level: 2 #}
{% for subscriber in group.subscribers %}
{# Level: 3 #}
<li>{{ subscriber.user.username }}</li>
{% endfor %}
</ol>
</li>
{% endfor %}
</ul>
{% endfor %}
Note:
I'm using jsonSerialize
to prepare the JSON-data, which includes multi-level iterating as well.
use JsonSerializable;
// ...
class Item implements JsonSerializable {
// ...
public function jsonSerialize() {
$subscribers = array();
$groups = $this->getGroups();
foreach ($groups as $group) {
foreach ($group->getSubscribers() as $subscriber) {
$subscribers[$subscriber->getId()] = array(
'userId' => $subscriber->getUser()->getId();
'username' => $subscriber->getUser()->getUsername();
);
}
}
return array(
'id' => $this->getId(),
'subscribers' => $subscribers
// ...
);
}
}
Is there a way to join the deeper associated data as well to reduce the number of SELECT
-queries once more (for twig and jsonSerialize())?