I have around 60 entities, all referring (manyToOne or oneToOne) to a "uber entity" called "project".
I'm developing a dashboard, so once selected the project I have to get all "children" entities referred to the project.
So I have:
class Entity1{
...
/**
* @ORM\OneToOne(targetEntity="Project")
*/
protected $project;
...
}
or
class Entity2{
...
/**
* @ORM\ManyToOne(targetEntity="Project")
*/
protected $project;
...
}
Of course I could do:
$entitiesToCheckStatusFor = ['Entity1', 'Entity2', ..., 'Entity60', ];
$entitiesStatus = [];
foreach ($entitiesToCheckStatusFor as $entityToCheckStatusFor){
$entitiesStatus[$entityToCheckStatusFor] = $em->getRepository('AppBundle:'.$entityToCheckStatusFor)->findByProject($project);
}
But it means 60 queries. It's far from elegant.
I basically need a JOIN between unrelated entities so that I can make a single query.
I tried something like:
$query = $em->createQuery('SELECT ai, pn, pb, pd p FROM AppBundle:Project p '
. 'LEFT OUTER JOIN AppBundle:ProjectNotification pn WITH p.id = pn.project '
. 'LEFT OUTER JOIN AppBundle:ProjectDetail pd WITH p.id = pd.project '
. 'LEFT OUTER JOIN AppBundle:ProjectBenefit pb WITH p.id = pb.project '
. 'LEFT OUTER JOIN AppBundle:ActionItem ai WITH p.id = ai.project '
. 'WHERE p.id = :projectid'
)->setParameter('projectid', $project->getId());
$mixedStuff = $query->getResult();
but it returns a lot of nulls when there are no entities:
array:20 [▼
0 => Project {#8285 ▶}
1 => null
2 => ProjectDetail {#3028 ▶}
3 => null
4 => ActionItem {#2978 ▶}
5 => null
6 => null
7 => ActionItem {#3191 ▶}
8 => null
9 => null
10 => ActionItem {#3200 ▶}
11 => null
12 => null
13 => ActionItem {#3205 ▶}
14 => null
15 => null
16 => ActionItem {#3210 ▶}
17 => null
18 => null
19 => ActionItem {#3214 ▶}
]
I could live with the nulls, but I was hoping to get rid of them. Besides in this case I was expecting to get 2 nulls (one for ProjectNotification
and another for ProjectBenefit
, while I have a lot. Why?
Any other advice?
Thank you!