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\CriterionInterface; |
15
|
|
|
use RDS\Hydrogen\Criteria\Relation; |
16
|
|
|
use RDS\Hydrogen\Query; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class RelationBuilder |
20
|
|
|
*/ |
21
|
|
|
class RelationBuilder extends WhereBuilder |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private static $relationId = 0; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param QueryBuilder $builder |
30
|
|
|
* @param CriterionInterface|Relation $relation |
31
|
|
|
* @return \Generator |
32
|
|
|
*/ |
33
|
|
|
public function apply($builder, CriterionInterface $relation): \Generator |
34
|
|
|
{ |
35
|
|
|
[$selections, $table, $alias] = [[], $this->meta->getTableName(), $relation->getAlias()]; |
|
|
|
|
36
|
|
|
|
37
|
|
|
$association = $this->getAssociation($this->meta, $relation->getRelation()->getName()); |
|
|
|
|
38
|
|
|
|
39
|
|
|
foreach ($this->getRelationsMappings($association) as $parent => $child) { |
40
|
|
|
$selections[$parent] = ['relatedTo' => $child, 'values' => []]; |
|
|
|
|
41
|
|
|
|
42
|
|
|
$builder->addSelect("RAW('$table', '$alias', '$parent') AS $parent"); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
yield function (iterable $result) use ($association, $selections, $relation) { |
|
|
|
|
46
|
|
|
$query = $relation->getQuery(); |
|
|
|
|
47
|
|
|
[$table, $alias] = [$this->meta($association['targetEntity'])->getTableName(), $query->getAlias()]; |
|
|
|
|
48
|
|
|
|
49
|
|
|
foreach ($result as $entity => $mappings) { |
50
|
|
|
foreach ($selections as $parent => $data) { |
51
|
|
|
$selections[$parent]['values'][] = $mappings[$parent]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach ($selections as $parent => ['relatedTo' => $child, 'values' => $values]) { |
56
|
|
|
$query->select(["RAW('$table', '$alias', '$child')" => $child]); |
|
|
|
|
57
|
|
|
$query = $query->whereIn(':' . $child, $values); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->execute($association['targetEntity'], $query); |
61
|
|
|
}; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ClassMetadata $metadata |
66
|
|
|
* @param string $relation |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
private function getAssociation(ClassMetadata $metadata, string $relation): array |
70
|
|
|
{ |
71
|
|
|
$association = $metadata->associationMappings[$relation] ?? null; |
72
|
|
|
|
73
|
|
|
if (! $association) { |
74
|
|
|
$error = 'Invalid relation name "%s" of entity %s'; |
75
|
|
|
throw new \LogicException(\sprintf($error, $relation, $metadata->name)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $association; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $association |
83
|
|
|
* @return \Generator |
84
|
|
|
*/ |
85
|
|
|
private function getRelationsMappings(array $association): \Generator |
86
|
|
|
{ |
87
|
|
|
yield from $association['isOwningSide'] |
88
|
|
|
? $this->getDirectRelationMappings($association) |
89
|
|
|
: $this->getInverseRelationMappings($association); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $association |
94
|
|
|
* @return \Generator |
95
|
|
|
*/ |
96
|
|
|
private function getDirectRelationMappings(array $association): \Generator |
97
|
|
|
{ |
98
|
|
|
$joins = $association['joinColumns'] ?? null; |
99
|
|
|
|
100
|
|
|
if (! $joins) { |
101
|
|
|
$error = 'Relation "%s" of entity %s should provide one or more join columns.'; |
102
|
|
|
throw new \LogicException(\sprintf($error, $association['fieldName'], $association['sourceEntity'])); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($joins as $join) { |
106
|
|
|
yield $join['name'] => $join['referencedColumnName']; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $association |
112
|
|
|
* @return \Generator |
113
|
|
|
*/ |
114
|
|
|
private function getInverseRelationMappings(array $association): \Generator |
115
|
|
|
{ |
116
|
|
|
$association = $this->getAssociation($this->meta($association['targetEntity']), $association['mappedBy']); |
117
|
|
|
|
118
|
|
|
foreach ($this->getDirectRelationMappings($association) as $parent => $child) { |
119
|
|
|
yield $child => $parent; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array $association |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
private function getJoinContext(array $association): array |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
if ($association['isOwningSide']) { |
130
|
|
|
return [ |
131
|
|
|
$this->getDirectRelationMappings($association), |
132
|
|
|
$this->meta, |
133
|
|
|
$this->meta($association['targetEntity']), |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return [ |
138
|
|
|
$this->getInverseRelationMappings($association), |
139
|
|
|
$this->meta($association['targetEntity']), |
140
|
|
|
$this->meta, |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.