1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors; |
9
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline; |
10
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants; |
11
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestor; |
12
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings; |
13
|
|
|
use Staudenmeir\LaravelCte\Eloquent\QueriesExpressions; |
14
|
|
|
|
15
|
|
|
trait HasRecursiveRelationships |
16
|
|
|
{ |
17
|
|
|
use HasOfDescendantsRelationships; |
18
|
|
|
use HasRecursiveRelationshipScopes; |
19
|
|
|
use QueriesExpressions; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the name of the parent key column. |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
670 |
|
public function getParentKeyName() |
27
|
|
|
{ |
28
|
670 |
|
return 'parent_id'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the qualified parent key column. |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
271 |
|
public function getQualifiedParentKeyName() |
37
|
|
|
{ |
38
|
271 |
|
return (new static())->getTable().'.'.$this->getParentKeyName(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the name of the local key column. |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
635 |
|
public function getLocalKeyName() |
47
|
|
|
{ |
48
|
635 |
|
return $this->getKeyName(); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the qualified local key column. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
308 |
|
public function getQualifiedLocalKeyName() |
57
|
|
|
{ |
58
|
308 |
|
return $this->qualifyColumn($this->getLocalKeyName()); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the name of the depth column. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
585 |
|
public function getDepthName() |
67
|
|
|
{ |
68
|
585 |
|
return 'depth'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the name of the path column. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
413 |
|
public function getPathName() |
77
|
|
|
{ |
78
|
413 |
|
return 'path'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the path separator. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
344 |
|
public function getPathSeparator() |
87
|
|
|
{ |
88
|
344 |
|
return '.'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the additional custom paths. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
264 |
|
public function getCustomPaths() |
97
|
|
|
{ |
98
|
264 |
|
return []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the name of the common table expression. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
585 |
|
public function getExpressionName() |
107
|
|
|
{ |
108
|
585 |
|
return 'laravel_cte'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the model's ancestors. |
113
|
|
|
* |
114
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors |
115
|
|
|
*/ |
116
|
60 |
|
public function ancestors() |
117
|
|
|
{ |
118
|
60 |
|
return $this->newAncestors( |
119
|
60 |
|
(new static())->newQuery(), |
|
|
|
|
120
|
|
|
$this, |
|
|
|
|
121
|
60 |
|
$this->getQualifiedParentKeyName(), |
122
|
60 |
|
$this->getLocalKeyName(), |
123
|
60 |
|
false |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the model's ancestors and itself. |
129
|
|
|
* |
130
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors |
131
|
|
|
*/ |
132
|
31 |
|
public function ancestorsAndSelf() |
133
|
|
|
{ |
134
|
31 |
|
return $this->newAncestors( |
135
|
31 |
|
(new static())->newQuery(), |
136
|
|
|
$this, |
|
|
|
|
137
|
31 |
|
$this->getQualifiedParentKeyName(), |
138
|
31 |
|
$this->getLocalKeyName(), |
139
|
31 |
|
true |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Instantiate a new Ancestors relationship. |
145
|
|
|
* |
146
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
147
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
148
|
|
|
* @param string $foreignKey |
149
|
|
|
* @param string $localKey |
150
|
|
|
* @param bool $andSelf |
151
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors |
152
|
|
|
*/ |
153
|
91 |
|
protected function newAncestors(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf) |
154
|
|
|
{ |
155
|
91 |
|
return new Ancestors($query, $parent, $foreignKey, $localKey, $andSelf); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the model's bloodline. |
160
|
|
|
* |
161
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline |
162
|
|
|
*/ |
163
|
25 |
|
public function bloodline() |
164
|
|
|
{ |
165
|
25 |
|
return $this->newBloodline( |
166
|
25 |
|
(new static())->newQuery(), |
167
|
|
|
$this, |
|
|
|
|
168
|
25 |
|
$this->getQualifiedParentKeyName(), |
169
|
25 |
|
$this->getLocalKeyName() |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Instantiate a new Bloodline relationship. |
175
|
|
|
* |
176
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
177
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
178
|
|
|
* @param string $foreignKey |
179
|
|
|
* @param string $localKey |
180
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline |
181
|
|
|
*/ |
182
|
25 |
|
protected function newBloodline(Builder $query, Model $parent, $foreignKey, $localKey) |
183
|
|
|
{ |
184
|
25 |
|
return new Bloodline($query, $parent, $foreignKey, $localKey); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the model's children. |
189
|
|
|
* |
190
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
191
|
|
|
*/ |
192
|
5 |
|
public function children() |
193
|
|
|
{ |
194
|
5 |
|
return $this->hasMany(static::class, $this->getParentKeyName(), $this->getLocalKeyName()); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the model's children and itself. |
199
|
|
|
* |
200
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants |
201
|
|
|
*/ |
202
|
5 |
|
public function childrenAndSelf() |
203
|
|
|
{ |
204
|
5 |
|
return $this->descendantsAndSelf()->whereDepth('<=', 1); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get the model's descendants. |
209
|
|
|
* |
210
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants |
211
|
|
|
*/ |
212
|
62 |
|
public function descendants() |
213
|
|
|
{ |
214
|
62 |
|
return $this->newDescendants( |
215
|
62 |
|
(new static())->newQuery(), |
216
|
|
|
$this, |
|
|
|
|
217
|
62 |
|
$this->getQualifiedParentKeyName(), |
218
|
62 |
|
$this->getLocalKeyName(), |
219
|
62 |
|
false |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get the model's descendants and itself. |
225
|
|
|
* |
226
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants |
227
|
|
|
*/ |
228
|
26 |
|
public function descendantsAndSelf() |
229
|
|
|
{ |
230
|
26 |
|
return $this->newDescendants( |
231
|
26 |
|
(new static())->newQuery(), |
232
|
|
|
$this, |
|
|
|
|
233
|
26 |
|
$this->getQualifiedParentKeyName(), |
234
|
26 |
|
$this->getLocalKeyName(), |
235
|
26 |
|
true |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Instantiate a new Descendants relationship. |
241
|
|
|
* |
242
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
243
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
244
|
|
|
* @param string $foreignKey |
245
|
|
|
* @param string $localKey |
246
|
|
|
* @param bool $andSelf |
247
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants |
248
|
|
|
*/ |
249
|
88 |
|
protected function newDescendants(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf) |
250
|
|
|
{ |
251
|
88 |
|
return new Descendants($query, $parent, $foreignKey, $localKey, $andSelf); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get the model's parent. |
256
|
|
|
* |
257
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
258
|
|
|
*/ |
259
|
5 |
|
public function parent() |
260
|
|
|
{ |
261
|
5 |
|
return $this->belongsTo(static::class, $this->getParentKeyName(), $this->getLocalKeyName()); |
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Get the model's parent and itself. |
266
|
|
|
* |
267
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors |
268
|
|
|
*/ |
269
|
5 |
|
public function parentAndSelf() |
270
|
|
|
{ |
271
|
5 |
|
return $this->ancestorsAndSelf()->whereDepth('>=', -1); |
|
|
|
|
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get the model's root ancestor. |
276
|
|
|
* |
277
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestor |
278
|
|
|
*/ |
279
|
25 |
|
public function rootAncestor() |
280
|
|
|
{ |
281
|
25 |
|
return $this->newRootAncestor( |
282
|
25 |
|
(new static())->newQuery(), |
283
|
|
|
$this, |
|
|
|
|
284
|
25 |
|
$this->getQualifiedParentKeyName(), |
285
|
25 |
|
$this->getLocalKeyName() |
286
|
|
|
); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Instantiate a new RootAncestor relationship. |
291
|
|
|
* |
292
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
293
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
294
|
|
|
* @param string $foreignKey |
295
|
|
|
* @param string $localKey |
296
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestor |
297
|
|
|
*/ |
298
|
25 |
|
protected function newRootAncestor(Builder $query, Model $parent, $foreignKey, $localKey) |
299
|
|
|
{ |
300
|
25 |
|
return new RootAncestor($query, $parent, $foreignKey, $localKey); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Get the model's siblings. |
305
|
|
|
* |
306
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings |
307
|
|
|
*/ |
308
|
35 |
|
public function siblings() |
309
|
|
|
{ |
310
|
35 |
|
return $this->newSiblings( |
311
|
35 |
|
(new static())->newQuery(), |
312
|
|
|
$this, |
|
|
|
|
313
|
35 |
|
$this->getQualifiedParentKeyName(), |
314
|
35 |
|
$this->getParentKeyName(), |
315
|
35 |
|
false |
316
|
|
|
); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Get the model's siblings and itself. |
321
|
|
|
* |
322
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings |
323
|
|
|
*/ |
324
|
30 |
|
public function siblingsAndSelf() |
325
|
|
|
{ |
326
|
30 |
|
return $this->newSiblings( |
327
|
30 |
|
(new static())->newQuery(), |
328
|
|
|
$this, |
|
|
|
|
329
|
30 |
|
$this->getQualifiedParentKeyName(), |
330
|
30 |
|
$this->getParentKeyName(), |
331
|
30 |
|
true |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Instantiate a new Siblings relationship. |
337
|
|
|
* |
338
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
339
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
340
|
|
|
* @param string $foreignKey |
341
|
|
|
* @param string $localKey |
342
|
|
|
* @param bool $andSelf |
343
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings |
344
|
|
|
*/ |
345
|
65 |
|
protected function newSiblings(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf) |
346
|
|
|
{ |
347
|
65 |
|
return new Siblings($query, $parent, $foreignKey, $localKey, $andSelf); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get the first segment of the model's path. |
352
|
|
|
* |
353
|
|
|
* @return string |
354
|
|
|
*/ |
355
|
60 |
|
public function getFirstPathSegment() |
356
|
|
|
{ |
357
|
60 |
|
$path = $this->attributes[$this->getPathName()]; |
358
|
|
|
|
359
|
60 |
|
return explode($this->getPathSeparator(), $path)[0]; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Determine whether the model's path is nested. |
364
|
|
|
* |
365
|
|
|
* @return bool |
366
|
|
|
*/ |
367
|
10 |
|
public function hasNestedPath() |
368
|
|
|
{ |
369
|
10 |
|
$path = $this->attributes[$this->getPathName()]; |
370
|
|
|
|
371
|
10 |
|
return Str::contains($path, $this->getPathSeparator()); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Determine if an attribute is an integer. |
376
|
|
|
* |
377
|
|
|
* @param string $attribute |
378
|
|
|
* @return bool |
379
|
|
|
*/ |
380
|
86 |
|
public function isIntegerAttribute($attribute) |
381
|
|
|
{ |
382
|
86 |
|
$casts = $this->getCasts(); |
|
|
|
|
383
|
|
|
|
384
|
86 |
|
return isset($casts[$attribute]) && in_array($casts[$attribute], ['int', 'integer']); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Create a new Eloquent query builder for the model. |
389
|
|
|
* |
390
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
391
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
392
|
|
|
*/ |
393
|
670 |
|
public function newEloquentBuilder($query) |
394
|
|
|
{ |
395
|
670 |
|
return new \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder($query); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Create a new Eloquent Collection instance. |
400
|
|
|
* |
401
|
|
|
* @param array $models |
402
|
|
|
* @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Collection |
403
|
|
|
*/ |
404
|
665 |
|
public function newCollection(array $models = []) |
405
|
|
|
{ |
406
|
665 |
|
return new Collection($models); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: