在一个查询中选择两个实体(使用连接)时,Doctrine返回一个扁平数组,其中每个实体都选择了一个元素。我想要每行一个数组元素,在该元素内部它包含在该行中选择的实体数组。
$qb = $this->_em->createQueryBuilder();
$qb
->select('competition')
->addSelect('competitor')
->from('Entity\Competition', 'competition')
->leftJoin('Entity\Competitor', 'competitor', \Doctrine\ORM\Query\Expr\Join::WITH, 'competition = competitor.competition')
->leftJoin('Entity\Athlete', 'a', \Doctrine\ORM\Query\Expr\Join::WITH, 'competitor.athlete = a')
->andWhere('co IS NULL')
->orWhere('co.athlete = :athleteId')
->setParameter('athleteId', $athleteId)
$data = $qb->getQuery()->getResult();
exit(var_dump($data));
此查询返回一个包含四个元素的数组:
[0] = Competition
[1] = Competitor
[2] = Competition
[3] = null
它返回的数据是正确的,但我希望它采用以下格式:
[0] = [
[competition] = Entity\Competition
[competitor] = Entity\Competitor
]
[1] = [
[competition] = Entity\Competition
[competitor] = null
]
我如何让Doctrine这样做?