I'm trying to do the following query:
$qb = $this->getEntityManager()->createQueryBuilder();
$referredUsers = $qb->select(['referred', 'referral', 'match'])
->from('ProjectContestBundle:UserParticipation', 'up')
->innerJoin('ProjectUserBundle:User', 'referred', 'WITH', 'up.user = referred')
->innerJoin('ProjectUserBundle:User', 'referral', 'WITH', 'up.referral = referral.referralCode')
->innerJoin('ProjectContestBundle:UserParticipation', 'upr', 'WITH', 'upr.user = referral')
->innerJoin('ProjectMatchBundle:Match', 'match', 'WITH', 'match.owner = referred')
->where('up.contest = :contest')->andWhere('upr.contest = :contest')
->setParameter('contest', $contest)
->getQuery()->getResult();
I was expecting as result an array of rows where each row is of type [User, User, Match]
, Doctrine however returns a plain array with a mixed content of entities.
I've then tried to make it simpler with:
$qb = $this->getEntityManager()->createQueryBuilder();
$referredUsers = $qb->select(['referred.id', 'referral.id', 'match.id'])
->from('ProjectContestBundle:UserParticipation', 'up')
->innerJoin('ProjectUserBundle:User', 'referred', 'WITH', 'up.user = referred')
->innerJoin('ProjectUserBundle:User', 'referral', 'WITH', 'up.referral = referral.referralCode')
->innerJoin('ProjectContestBundle:UserParticipation', 'upr', 'WITH', 'upr.user = referral')
->innerJoin('ProjectMatchBundle:Match', 'match', 'WITH', 'match.owner = referred')
->where('up.contest = :contest')->andWhere('upr.contest = :contest')
->setParameter('contest', $contest)
->getQuery()->getResult();
which results in [{"id":1},{"id":2},{"id":3},{"id":4}]
. If I do run the query on the database it works fine:
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;
returns:
+-----+-----+-----+
| ID0 | ID1 | ID2 |
+-----+-----+-----+
| 1 | 9 | 1 |
| 1 | 9 | 2 |
| 1 | 9 | 3 |
| 10 | 9 | 4 |
+-----+-----+-----+
so I tried to add some as
and:
$referredUsers = $qb->select(['referred.id as r1', 'referral.id as r2', 'match.id as m'])
->from('ProjectContestBundle:UserParticipation', 'up')
->innerJoin('ProjectUserBundle:User', 'referred', 'WITH', 'up.user = referred')
->innerJoin('ProjectUserBundle:User', 'referral', 'WITH', 'up.referral = referral.referralCode')
->innerJoin('ProjectContestBundle:UserParticipation', 'upr', 'WITH', 'upr.user = referral')
->innerJoin('ProjectMatchBundle:Match', 'match', 'WITH', 'match.owner = referred')
->where('up.contest = :contest')->andWhere('upr.contest = :contest')
->setParameter('contest', $contest)
->getQuery()->getArrayResult();
and the result is correct!
[{"r1":1,"r2":9,"m":1},{"r1":1,"r2":9,"m":2},{"r1":1,"r2":9,"m":3},{"r1":10,"r2":9,"m":4}]
If I try to go back with the non id version, leaving the as
I still get back an array of rows where each row is a different entity, however I would like to have a row of [User, User, Match]
as the select shows (the same as the MySql raw result, just with entities).
referral
andreferralCode
are a columns, but I think should be a relationship userParticipation to user