1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Hydrogen package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace RDS\Hydrogen\Processor\DatabaseProcessor; |
11
|
|
|
|
12
|
|
|
use Doctrine\ORM\QueryBuilder; |
13
|
|
|
use RDS\Hydrogen\Criteria\Common\Field; |
14
|
|
|
use RDS\Hydrogen\Criteria\CriterionInterface; |
15
|
|
|
use RDS\Hydrogen\Criteria\Join; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class JoinBuilder |
19
|
|
|
*/ |
20
|
|
|
class JoinBuilder extends WhereBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $relations = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param QueryBuilder $builder |
29
|
|
|
* @param CriterionInterface|Join $join |
30
|
|
|
* @return iterable|null |
31
|
|
|
*/ |
32
|
2 |
|
public function apply($builder, CriterionInterface $join): ?iterable |
33
|
|
|
{ |
34
|
2 |
|
[$entity, $alias] = $this->joinAll($builder, $join); |
|
|
|
|
35
|
|
|
|
36
|
2 |
|
if ($join->hasJoinQuery()) { |
|
|
|
|
37
|
|
|
$repository = $this->processor->getProcessor($entity)->getRepository(); |
38
|
|
|
|
39
|
|
|
$query = $this->query->create() |
40
|
|
|
->from($repository) |
41
|
|
|
->withAlias($alias); |
42
|
|
|
|
43
|
|
|
yield $join->getJoinQuery($query); |
|
|
|
|
44
|
|
|
} |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param QueryBuilder $builder |
49
|
|
|
* @param Join $join |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
2 |
|
private function joinAll(QueryBuilder $builder, Join $join): array |
53
|
|
|
{ |
54
|
2 |
|
[$alias, $relation] = [$join->getQueryAlias(), []]; |
|
|
|
|
55
|
|
|
|
56
|
2 |
|
foreach ($join->getRelations($this->processor) as $isLast => $relation) { |
57
|
|
|
// Is the relation already loaded in current query execution |
58
|
2 |
|
$exists = $this->hasAlias($relation); |
59
|
|
|
|
60
|
|
|
// Resolve relation alias |
61
|
2 |
|
$relationAlias = $isLast && $join->hasJoinQuery() |
62
|
|
|
? $this->getAlias($relation) |
63
|
2 |
|
: $this->getCachedAlias($relation); |
64
|
|
|
|
65
|
2 |
|
if (! $exists) { |
66
|
|
|
// Create join |
67
|
2 |
|
$relationField = Field::new($relation['fieldName'])->toString($alias); |
|
|
|
|
68
|
2 |
|
$this->join($builder, $join, $relationField, $relationAlias); |
69
|
|
|
|
70
|
|
|
// Add join to selection statement |
71
|
2 |
|
$builder->addSelect($relationAlias); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Shift parent |
75
|
2 |
|
$alias = $relationAlias; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
return [$relation['targetEntity'], $alias]; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param QueryBuilder $builder |
83
|
|
|
* @param Join $join |
84
|
|
|
* @param string $field |
85
|
|
|
* @param string $relationAlias |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
2 |
|
private function join(QueryBuilder $builder, Join $join, string $field, string $relationAlias): void |
89
|
|
|
{ |
90
|
2 |
|
switch ($join->getType()) { |
91
|
2 |
|
case Join::TYPE_JOIN: |
92
|
|
|
$builder->join($field, $relationAlias); |
93
|
|
|
break; |
94
|
|
|
|
95
|
2 |
|
case Join::TYPE_INNER_JOIN: |
96
|
|
|
$builder->innerJoin($field, $relationAlias); |
97
|
|
|
break; |
98
|
|
|
|
99
|
2 |
|
case Join::TYPE_LEFT_JOIN: |
100
|
2 |
|
$builder->leftJoin($field, $relationAlias); |
101
|
2 |
|
break; |
102
|
|
|
} |
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $relation |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
2 |
|
private function getKey(array $relation): string |
110
|
|
|
{ |
111
|
2 |
|
return $relation['sourceEntity'] . '_' . $relation['targetEntity']; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $relation |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
2 |
|
private function hasAlias(array $relation): bool |
119
|
|
|
{ |
120
|
2 |
|
$key = $this->getKey($relation); |
121
|
|
|
|
122
|
2 |
|
return isset($this->relations[$key]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $relation |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
2 |
|
private function getCachedAlias(array $relation): string |
130
|
|
|
{ |
131
|
2 |
|
$key = $this->getKey($relation); |
132
|
|
|
|
133
|
2 |
|
if (! isset($this->relations[$key])) { |
134
|
2 |
|
return $this->relations[$key] = |
135
|
2 |
|
$this->query->createAlias( |
136
|
2 |
|
$relation['sourceEntity'], |
137
|
2 |
|
$relation['targetEntity'] |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->relations[$key]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $relation |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
private function getAlias(array $relation): string |
149
|
|
|
{ |
150
|
|
|
return $this->query->createAlias( |
151
|
|
|
$relation['sourceEntity'], |
152
|
|
|
$relation['targetEntity'] |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.