1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
8
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits\IsOfDescendantsRelation; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model |
12
|
|
|
* @extends HasMany<TRelatedModel> |
13
|
|
|
*/ |
14
|
|
|
class HasManyOfDescendants extends HasMany |
15
|
|
|
{ |
16
|
|
|
use IsOfDescendantsRelation; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create a new has many of descendants relationship instance. |
20
|
|
|
* |
21
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
22
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
23
|
|
|
* @param string $foreignKey |
24
|
|
|
* @param string $localKey |
25
|
|
|
* @param bool $andSelf |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
95 |
|
public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf) |
29
|
|
|
{ |
30
|
95 |
|
$this->andSelf = $andSelf; |
31
|
|
|
|
32
|
95 |
|
parent::__construct($query, $parent, $foreignKey, $localKey); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set the where clause on the recursive expression query. |
37
|
|
|
* |
38
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
58 |
|
public function addExpressionWhereConstraints(Builder $query) |
42
|
|
|
{ |
43
|
58 |
|
$column = $this->andSelf ? $this->parent->getLocalKeyName() : $this->parent->getParentKeyName(); |
44
|
|
|
|
45
|
58 |
|
$query->where( |
46
|
58 |
|
$column, |
47
|
58 |
|
'=', |
48
|
58 |
|
$this->parent->{$this->parent->getLocalKeyName()} |
49
|
58 |
|
)->whereNotNull($column); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the local key name for an eager load of the relation. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
25 |
|
public function getEagerLoadingLocalKeyName() |
58
|
|
|
{ |
59
|
25 |
|
return $this->getLocalKeyName(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the foreign key name for an eager load of the relation. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
20 |
|
public function getEagerLoadingForeignKeyName() |
68
|
|
|
{ |
69
|
20 |
|
return $this->getForeignKeyName(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the local key name for the recursion expression. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
85 |
|
public function getExpressionLocalKeyName() |
78
|
|
|
{ |
79
|
85 |
|
return $this->getLocalKeyName(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the foreign key name for the recursion expression. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
85 |
|
public function getExpressionForeignKeyName() |
88
|
|
|
{ |
89
|
85 |
|
return $this->foreignKey; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|