Dynamically eager loading deep relationships with Doctrine
<p>I'm currently working on an API using the following stack;</p>

<ul>
<li>Symfony (3)</li>
<li>FOSRestBundle</li>
<li>Fractal</li>
</ul>

<p>I'm wanting to integrate the ability to specify, via query parameter, which relationships to include when retrieving an entity/collection, e.g;</p>

<pre><code>[GET] /users?include=friends.addresses
</code></pre>

<p><a href="http://fractal.thephpleague.com/transformers/#including-data" rel="nofollow">Fractal comes with the ability to handle includes</a> however, as this happens around the serialization point of the response building, each related entity is retrieved via lazy loading, thus triggering additional queries. </p>

<p>Is there a way to tell Doctrine, when retrieving a collection, to dynamically also retrieve relationships specified? Ive seen <a href="https://doctrine-orm.readthedocs.io/en/latest/reference/dql-doctrine-query-language.html#temporarily-change-fetch-mode-in-dql" rel="nofollow">the following from the Doctrine docs which shows how to dynamically change the fetch mode</a> however this only seems to work with associations on the target entity (<code>friends</code> in the example above) and not deeper relations (<code>addresses</code> of <code>friends</code> in the example).</p>

<p>Thanks!</p>