|
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\Mapping\ClassMetadata; |
|
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
14
|
|
|
use RDS\Hydrogen\Criteria\Common\Field; |
|
15
|
|
|
use RDS\Hydrogen\Criteria\CriterionInterface; |
|
16
|
|
|
use RDS\Hydrogen\Criteria\Join; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class JoinBuilder |
|
20
|
|
|
*/ |
|
21
|
|
|
class JoinBuilder extends WhereBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $relations = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param QueryBuilder $builder |
|
30
|
|
|
* @param CriterionInterface|Join $join |
|
31
|
|
|
* @return iterable|null |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function apply($builder, CriterionInterface $join): ?iterable |
|
34
|
|
|
{ |
|
35
|
2 |
|
[$entity, $alias] = $this->joinAll($builder, $join); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
2 |
|
if ($join->hasJoinQuery()) { |
|
|
|
|
|
|
38
|
|
|
$repository = $this->processor->getProcessor($entity)->getRepository(); |
|
39
|
|
|
|
|
40
|
|
|
$query = $this->query->create() |
|
41
|
|
|
->from($repository) |
|
42
|
|
|
->withAlias($alias); |
|
43
|
|
|
|
|
44
|
|
|
yield $join->getJoinQuery($query); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param QueryBuilder $builder |
|
50
|
|
|
* @param Join $join |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
2 |
|
private function joinAll(QueryBuilder $builder, Join $join): array |
|
54
|
|
|
{ |
|
55
|
2 |
|
[$alias, $relation] = [$join->getQueryAlias(), []]; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
2 |
|
foreach ($join->getRelations($this->processor) as $isLast => $relation) { |
|
58
|
|
|
|
|
59
|
|
|
// Is the relation already loaded in current query execution |
|
60
|
2 |
|
$exists = $this->hasAlias($relation); |
|
61
|
|
|
|
|
62
|
|
|
// Resolve relation alias |
|
63
|
2 |
|
$relationAlias = $isLast && $join->hasJoinQuery() |
|
64
|
|
|
? $this->getAlias($relation) |
|
65
|
2 |
|
: $this->getCachedAlias($relation); |
|
66
|
|
|
|
|
67
|
2 |
|
if (! $exists) { |
|
68
|
|
|
// Create join |
|
69
|
2 |
|
$relationField = Field::new($relation['fieldName'])->toString($alias); |
|
|
|
|
|
|
70
|
2 |
|
$this->join($builder, $join, $relationField, $relationAlias); |
|
71
|
|
|
|
|
72
|
|
|
// Add join to selection statement |
|
73
|
2 |
|
$builder->addSelect($relationAlias); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Shift parent |
|
77
|
2 |
|
$alias = $relationAlias; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
return [$relation['targetEntity'], $alias]; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param QueryBuilder $builder |
|
85
|
|
|
* @param Join $join |
|
86
|
|
|
* @param string $field |
|
87
|
|
|
* @param string $relationAlias |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
2 |
|
private function join(QueryBuilder $builder, Join $join, string $field, string $relationAlias): void |
|
91
|
|
|
{ |
|
92
|
2 |
|
switch ($join->getType()) { |
|
93
|
2 |
|
case Join::TYPE_JOIN: |
|
94
|
1 |
|
$builder->join($field, $relationAlias); |
|
95
|
1 |
|
break; |
|
96
|
|
|
|
|
97
|
1 |
|
case Join::TYPE_INNER_JOIN: |
|
98
|
|
|
$builder->innerJoin($field, $relationAlias); |
|
99
|
|
|
break; |
|
100
|
|
|
|
|
101
|
1 |
|
case Join::TYPE_LEFT_JOIN: |
|
102
|
1 |
|
$builder->leftJoin($field, $relationAlias); |
|
103
|
1 |
|
break; |
|
104
|
|
|
} |
|
105
|
2 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param array $relation |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
2 |
|
private function getKey(array $relation): string |
|
112
|
|
|
{ |
|
113
|
2 |
|
return $relation['sourceEntity'] . '_' . $relation['targetEntity']; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param array $relation |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
2 |
|
private function hasAlias(array $relation): bool |
|
121
|
|
|
{ |
|
122
|
2 |
|
$key = $this->getKey($relation); |
|
123
|
|
|
|
|
124
|
2 |
|
return isset($this->relations[$key]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param array $relation |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
2 |
|
private function getCachedAlias(array $relation): string |
|
132
|
|
|
{ |
|
133
|
2 |
|
$key = $this->getKey($relation); |
|
134
|
|
|
|
|
135
|
2 |
|
if (! isset($this->relations[$key])) { |
|
136
|
2 |
|
return $this->relations[$key] = |
|
137
|
2 |
|
$this->query->createAlias( |
|
138
|
2 |
|
$relation['sourceEntity'], |
|
139
|
2 |
|
$relation['targetEntity'] |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
return $this->relations[$key]; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param array $relation |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
private function getAlias(array $relation): string |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->query->createAlias( |
|
153
|
|
|
$relation['sourceEntity'], |
|
154
|
|
|
$relation['targetEntity'] |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
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.