1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
9
|
|
|
use Illuminate\Database\Query\Expression; |
10
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Traits\IsRecursiveRelation; |
11
|
|
|
|
12
|
|
|
class Ancestors extends BelongsToMany |
13
|
|
|
{ |
14
|
|
|
use IsRecursiveRelation { |
15
|
|
|
buildDictionary as baseBuildDictionary; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Set the base constraints on the relation query. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
99 |
|
public function addConstraints() |
24
|
|
|
{ |
25
|
99 |
|
if (static::$constraints) { |
26
|
49 |
|
$column = $this->andSelf ? $this->getQualifiedParentKeyName() : $this->getQualifiedRelatedPivotKeyName(); |
27
|
|
|
|
28
|
49 |
|
$constraint = function (Builder $query) use ($column) { |
29
|
49 |
|
$query->where( |
30
|
|
|
$column, |
31
|
|
|
'=', |
32
|
49 |
|
$this->parent->{$this->parentKey} |
33
|
|
|
); |
34
|
|
|
}; |
35
|
|
|
|
36
|
49 |
|
$this->addExpression($constraint); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the constraints for an eager load of the relation. |
42
|
|
|
* |
43
|
|
|
* @param array $models |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
32 |
|
public function addEagerConstraints(array $models) |
47
|
|
|
{ |
48
|
32 |
|
$whereIn = $this->whereInMethod($this->parent, $this->parentKey); |
49
|
|
|
|
50
|
32 |
|
$column = $this->andSelf ? $this->getQualifiedParentKeyName() : $this->getQualifiedRelatedPivotKeyName(); |
51
|
|
|
|
52
|
32 |
|
$keys = $this->getKeys($models, $this->parentKey); |
53
|
|
|
|
54
|
32 |
|
$constraint = function (Builder $query) use ($models, $whereIn, $column, $keys) { |
|
|
|
|
55
|
32 |
|
$query->$whereIn($column, $keys); |
56
|
|
|
}; |
57
|
|
|
|
58
|
32 |
|
$this->addExpression($constraint, null, null, $this->andSelf ? 'unionAll' : 'union'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Build model dictionary. |
63
|
|
|
* |
64
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
32 |
|
protected function buildDictionary(Collection $results) |
68
|
|
|
{ |
69
|
32 |
|
if ($this->andSelf) { |
70
|
16 |
|
return $this->baseBuildDictionary($results); |
71
|
|
|
} |
72
|
|
|
|
73
|
16 |
|
$dictionary = []; |
74
|
|
|
|
75
|
16 |
|
$depthName = $this->related->getDepthName(); |
76
|
|
|
|
77
|
16 |
|
$firstLevelResults = $results->where($depthName, '=', -1)->groupBy($this->parentKey); |
78
|
|
|
|
79
|
16 |
|
foreach ($results as $result) { |
80
|
16 |
|
$keys = []; |
81
|
|
|
|
82
|
16 |
|
if ($result->$depthName < -1) { |
83
|
16 |
|
foreach ($firstLevelResults[$result->getFirstPathSegment()] as $model) { |
84
|
16 |
|
$keys[] = $model->{$this->accessor}->{$this->relatedPivotKey}; |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
16 |
|
$keys[] = $result->{$this->accessor}->{$this->relatedPivotKey}; |
88
|
|
|
} |
89
|
|
|
|
90
|
16 |
|
foreach ($keys as $key) { |
91
|
16 |
|
$dictionary[$key][] = $result; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
16 |
|
return $dictionary; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add the constraints for a relationship query. |
100
|
|
|
* |
101
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
102
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
103
|
|
|
* @param array|mixed $columns |
104
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
105
|
|
|
*/ |
106
|
18 |
|
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) |
107
|
|
|
{ |
108
|
18 |
|
if ($query->getQuery()->from === $parentQuery->getQuery()->from) { |
109
|
12 |
|
return $this->getRelationExistenceQueryForSelfRelation($query, $columns); |
110
|
|
|
} |
111
|
|
|
|
112
|
6 |
|
$first = $this->andSelf |
113
|
3 |
|
? $query->getQuery()->from . '.' . $this->parentKey |
114
|
3 |
|
: $this->getQualifiedRelatedPivotKeyName(); |
115
|
|
|
|
116
|
6 |
|
$constraint = function (Builder $query) use ($first) { |
117
|
6 |
|
$query->whereColumn( |
118
|
|
|
$first, |
119
|
|
|
'=', |
120
|
6 |
|
$this->getQualifiedParentKeyName() |
121
|
|
|
); |
122
|
|
|
}; |
123
|
|
|
|
124
|
6 |
|
return $this->addExpression($constraint, $query->select($columns)); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Add the constraints for a relationship query on the same table. |
129
|
|
|
* |
130
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
131
|
|
|
* @param array|mixed $columns |
132
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
133
|
|
|
*/ |
134
|
12 |
|
public function getRelationExistenceQueryForSelfRelation( |
135
|
|
|
Builder $query, |
136
|
|
|
$columns = ['*'] |
137
|
|
|
): Builder { |
138
|
12 |
|
if ($columns instanceof Expression) { |
139
|
9 |
|
$columns = $this->replaceTableHash($query, $columns); |
140
|
|
|
} |
141
|
|
|
|
142
|
12 |
|
$table = $this->getRelationCountHash(); |
143
|
|
|
|
144
|
12 |
|
$from = $query->getModel()->getTable() . ' as ' . $table; |
145
|
|
|
|
146
|
12 |
|
$query->getModel()->setTable($table); |
147
|
|
|
|
148
|
12 |
|
$first = $this->andSelf |
149
|
6 |
|
? "$table.$this->parentKey" |
150
|
6 |
|
: "$this->table.$this->relatedPivotKey"; |
151
|
|
|
|
152
|
12 |
|
$constraint = function (Builder $query) use ($first) { |
153
|
12 |
|
$query->whereColumn( |
154
|
|
|
$first, |
155
|
|
|
'=', |
156
|
12 |
|
$this->getQualifiedParentKeyName() |
157
|
|
|
); |
158
|
|
|
}; |
159
|
|
|
|
160
|
12 |
|
return $this->addExpression($constraint, $query->select($columns), $from); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Add a recursive expression to the query. |
165
|
|
|
* |
166
|
|
|
* @param callable $constraint |
167
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|null $query |
168
|
|
|
* @param string|null $from |
169
|
|
|
* @param string $union |
170
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
171
|
|
|
*/ |
172
|
99 |
|
protected function addExpression( |
173
|
|
|
callable $constraint, |
174
|
|
|
Builder $query = null, |
175
|
|
|
string $from = null, |
176
|
|
|
string $union = 'unionAll' |
177
|
|
|
): Builder { |
178
|
99 |
|
$query = $query ?: $this->query; |
179
|
|
|
|
180
|
99 |
|
$initialDepth = $this->andSelf ? 0 : -1; |
181
|
|
|
|
182
|
99 |
|
return $query->withRelationshipExpression( |
183
|
|
|
'asc', |
184
|
|
|
$constraint, |
185
|
|
|
$initialDepth, |
186
|
|
|
$from, |
187
|
|
|
null, |
188
|
|
|
$union |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
This check looks for imports that have been defined, but are not used in the scope.