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
|
|
|
// Resolve relation alias |
58
|
2 |
|
$relationAlias = $isLast && $join->hasJoinQuery() |
59
|
|
|
? $this->getAlias($relation) |
60
|
2 |
|
: $this->getCachedAlias($relation); |
61
|
|
|
|
62
|
|
|
// Create join |
63
|
2 |
|
$relationField = Field::new($relation['fieldName'])->toString($alias); |
|
|
|
|
64
|
2 |
|
$this->join($builder, $join, $relationField, $relationAlias); |
65
|
|
|
|
66
|
|
|
// Add join to selection statement |
67
|
2 |
|
$builder->addSelect($relationAlias); |
68
|
|
|
|
69
|
|
|
// Shift parent |
70
|
2 |
|
$alias = $relationAlias; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
return [$relation['targetEntity'], $alias]; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param QueryBuilder $builder |
78
|
|
|
* @param Join $join |
79
|
|
|
* @param string $field |
80
|
|
|
* @param string $relationAlias |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
2 |
|
private function join(QueryBuilder $builder, Join $join, string $field, string $relationAlias): void |
84
|
|
|
{ |
85
|
2 |
|
switch ($join->getType()) { |
86
|
2 |
|
case Join::TYPE_JOIN: |
87
|
|
|
$builder->join($field, $relationAlias); |
88
|
|
|
break; |
89
|
|
|
|
90
|
2 |
|
case Join::TYPE_INNER_JOIN: |
91
|
|
|
$builder->innerJoin($field, $relationAlias); |
92
|
|
|
break; |
93
|
|
|
|
94
|
2 |
|
case Join::TYPE_LEFT_JOIN: |
95
|
2 |
|
$builder->leftJoin($field, $relationAlias); |
96
|
2 |
|
break; |
97
|
|
|
} |
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $relation |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
2 |
|
private function getKey(array $relation): string |
105
|
|
|
{ |
106
|
2 |
|
return $relation['sourceEntity'] . '_' . $relation['targetEntity']; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array $relation |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
2 |
|
private function getCachedAlias(array $relation): string |
114
|
|
|
{ |
115
|
2 |
|
$key = $this->getKey($relation); |
116
|
|
|
|
117
|
2 |
|
if (! isset($this->relations[$key])) { |
118
|
2 |
|
return $this->relations[$key] = |
119
|
2 |
|
$this->query->createAlias( |
120
|
2 |
|
$relation['sourceEntity'], |
121
|
2 |
|
$relation['targetEntity'] |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->relations[$key]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $relation |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
private function getAlias(array $relation): string |
133
|
|
|
{ |
134
|
|
|
return $this->query->createAlias( |
135
|
|
|
$relation['sourceEntity'], |
136
|
|
|
$relation['targetEntity'] |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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.