Select multiple entities doctrine
<p>I'm trying to do the following query:</p>

<pre><code>$qb = $this-&gt;getEntityManager()-&gt;createQueryBuilder();
$referredUsers = $qb-&gt;select(['referred', 'referral', 'match'])
    -&gt;from('ProjectContestBundle:UserParticipation', 'up')
    -&gt;innerJoin('ProjectUserBundle:User', 'referred', 'WITH', 'up.user = referred')
    -&gt;innerJoin('ProjectUserBundle:User', 'referral', 'WITH', 'up.referral = referral.referralCode')
    -&gt;innerJoin('ProjectContestBundle:UserParticipation', 'upr', 'WITH', 'upr.user = referral')
    -&gt;innerJoin('ProjectMatchBundle:Match', 'match', 'WITH', 'match.owner = referred')
    -&gt;where('up.contest = :contest')-&gt;andWhere('upr.contest = :contest')
    -&gt;setParameter('contest', $contest)
    -&gt;getQuery()-&gt;getResult();
</code></pre>

<p>I was expecting as result an array of rows where each row is of type <code>[User, User, Match]</code>, Doctrine however returns a plain array with a mixed content of entities.</p>

<p>I've then tried to make it simpler with:</p>

<pre><code>$qb = $this-&gt;getEntityManager()-&gt;createQueryBuilder();
$referredUsers = $qb-&gt;select(['referred.id', 'referral.id', 'match.id'])
    -&gt;from('ProjectContestBundle:UserParticipation', 'up')
    -&gt;innerJoin('ProjectUserBundle:User', 'referred', 'WITH', 'up.user = referred')
    -&gt;innerJoin('ProjectUserBundle:User', 'referral', 'WITH', 'up.referral = referral.referralCode')
    -&gt;innerJoin('ProjectContestBundle:UserParticipation', 'upr', 'WITH', 'upr.user = referral')
    -&gt;innerJoin('ProjectMatchBundle:Match', 'match', 'WITH', 'match.owner = referred')
    -&gt;where('up.contest = :contest')-&gt;andWhere('upr.contest = :contest')
    -&gt;setParameter('contest', $contest)
    -&gt;getQuery()-&gt;getResult();
</code></pre>

<p>which results in <code>[{"id":1},{"id":2},{"id":3},{"id":4}]</code>. If I do run the query on the database it works fine:</p>

<pre><code>SELECT u0_.ID AS ID0, u1_.ID AS ID1, m2_.ID AS ID2 FROM UserParticipation u3_ INNER JOIN users u0_ ON (u3_.user_id = u0_.ID) INNER JOIN users u1_ ON (u3_.l = u1_.referralCode) INNER JOIN UserParticipation u4_ ON (u4_.user_id = u1_.ID) INNER JOIN matches m2_ ON (m2_.ownerID = u0_.ID) AND (m2_.deletedAt IS NULL) WHERE u3_.contest_id = 1 AND u4_.contest_id = 1;
</code></pre>

<p>returns:</p>

<pre><code>+-----+-----+-----+
| ID0 | ID1 | ID2 |
+-----+-----+-----+
|   1 |   9 |   1 |
|   1 |   9 |   2 |
|   1 |   9 |   3 |
|  10 |   9 |   4 |
+-----+-----+-----+
</code></pre>

<p>so I tried to add some <code>as</code> and:</p>

<pre><code>$referredUsers = $qb-&gt;select(['referred.id as r1', 'referral.id as r2', 'match.id as m'])
    -&gt;from('ProjectContestBundle:UserParticipation', 'up')
    -&gt;innerJoin('ProjectUserBundle:User', 'referred', 'WITH', 'up.user = referred')
    -&gt;innerJoin('ProjectUserBundle:User', 'referral', 'WITH', 'up.referral = referral.referralCode')
    -&gt;innerJoin('ProjectContestBundle:UserParticipation', 'upr', 'WITH', 'upr.user = referral')
    -&gt;innerJoin('ProjectMatchBundle:Match', 'match', 'WITH', 'match.owner = referred')
    -&gt;where('up.contest = :contest')-&gt;andWhere('upr.contest = :contest')
    -&gt;setParameter('contest', $contest)
    -&gt;getQuery()-&gt;getArrayResult();
</code></pre>

<p>and the result is correct!</p>

<pre><code>[{"r1":1,"r2":9,"m":1},{"r1":1,"r2":9,"m":2},{"r1":1,"r2":9,"m":3},{"r1":10,"r2":9,"m":4}]
</code></pre>

<p>If I try to go back with the non id version, leaving the <code>as</code> I still get back an array of rows where each row is a different entity, however I would like to have a row of <code>[User, User, Match]</code> as the select shows (the same as the MySql raw result, just with entities).</p>