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\BelongsToMany; |
8
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits\IsOfDescendantsRelation; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model |
12
|
|
|
* @extends BelongsToMany<TRelatedModel> |
13
|
|
|
*/ |
14
|
|
|
class BelongsToManyOfDescendants extends BelongsToMany |
15
|
|
|
{ |
16
|
|
|
use IsOfDescendantsRelation { |
|
|
|
|
17
|
|
|
addConstraints as baseAddConstraints; |
18
|
|
|
getRelationExistenceQuery as baseGetRelationExistenceQuery; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create a new belongs to many of descendants relationship instance. |
23
|
|
|
* |
24
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
25
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
26
|
|
|
* @param string $table |
27
|
|
|
* @param string $foreignPivotKey |
28
|
|
|
* @param string $relatedPivotKey |
29
|
|
|
* @param string $parentKey |
30
|
|
|
* @param string $relatedKey |
31
|
|
|
* @param bool $andSelf |
32
|
|
|
*/ |
33
|
255 |
|
public function __construct( |
34
|
|
|
Builder $query, |
35
|
|
|
Model $parent, |
36
|
|
|
$table, |
37
|
|
|
$foreignPivotKey, |
38
|
|
|
$relatedPivotKey, |
39
|
|
|
$parentKey, |
40
|
|
|
$relatedKey, |
41
|
|
|
$andSelf |
42
|
|
|
) { |
43
|
255 |
|
$this->andSelf = $andSelf; |
44
|
|
|
|
45
|
255 |
|
parent::__construct($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set the base constraints on the relation query. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
255 |
|
public function addConstraints() |
54
|
|
|
{ |
55
|
255 |
|
$this->performJoin(); |
56
|
|
|
|
57
|
255 |
|
$this->baseAddConstraints(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set the where clause on the recursive expression query. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
159 |
|
protected function addExpressionWhereConstraints(Builder $query) |
67
|
|
|
{ |
68
|
159 |
|
$column = $this->andSelf ? $this->parent->getLocalKeyName() : $this->parent->getParentKeyName(); |
69
|
|
|
|
70
|
159 |
|
$query->where( |
71
|
159 |
|
$column, |
72
|
159 |
|
'=', |
73
|
159 |
|
$this->parent->{$this->parentKey} |
74
|
159 |
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the local key name for an eager load of the relation. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
60 |
|
public function getEagerLoadingLocalKeyName() |
83
|
|
|
{ |
84
|
60 |
|
return $this->parentKey; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the foreign key name for an eager load of the relation. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
60 |
|
public function getEagerLoadingForeignKeyName() |
93
|
|
|
{ |
94
|
60 |
|
return $this->foreignPivotKey; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the accessor for an eager load of the relation. |
99
|
|
|
* |
100
|
|
|
* @return string|null |
101
|
|
|
*/ |
102
|
60 |
|
public function getEagerLoadingAccessor() |
103
|
|
|
{ |
104
|
60 |
|
return $this->accessor; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Add the constraints for a relationship query. |
109
|
|
|
* |
110
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
111
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parentQuery |
112
|
|
|
* @param array|mixed $columns |
113
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
114
|
|
|
*/ |
115
|
36 |
|
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) |
116
|
|
|
{ |
117
|
36 |
|
$this->performJoin($query); |
118
|
|
|
|
119
|
36 |
|
return $this->baseGetRelationExistenceQuery($query, $parentQuery, $columns); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the local key name for the recursion expression. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
225 |
|
public function getExpressionLocalKeyName() |
128
|
|
|
{ |
129
|
225 |
|
return $this->parentKey; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the foreign key name for the recursion expression. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
225 |
|
public function getExpressionForeignKeyName() |
138
|
|
|
{ |
139
|
225 |
|
return $this->foreignPivotKey; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|