1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model |
10
|
|
|
* @extends BelongsToManyOfDescendants<TRelatedModel> |
11
|
|
|
*/ |
12
|
|
|
class MorphToManyOfDescendants extends BelongsToManyOfDescendants |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The type of the polymorphic relation. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $morphType; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The class name of the morph type constraint. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $morphClass; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Indicates if we are connecting the inverse of the relation. |
30
|
|
|
* |
31
|
|
|
* This primarily affects the morphClass constraint. |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $inverse; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new morph to many of descendants relationship instance. |
39
|
|
|
* |
40
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
41
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
42
|
|
|
* @param string $name |
43
|
|
|
* @param string $table |
44
|
|
|
* @param string $foreignPivotKey |
45
|
|
|
* @param string $relatedPivotKey |
46
|
|
|
* @param string $parentKey |
47
|
|
|
* @param string $relatedKey |
48
|
|
|
* @param bool $inverse |
49
|
|
|
* @param bool $andSelf |
50
|
|
|
*/ |
51
|
170 |
|
public function __construct( |
52
|
|
|
Builder $query, |
53
|
|
|
Model $parent, |
54
|
|
|
$name, |
55
|
|
|
$table, |
56
|
|
|
$foreignPivotKey, |
57
|
|
|
$relatedPivotKey, |
58
|
|
|
$parentKey, |
59
|
|
|
$relatedKey, |
60
|
|
|
$inverse, |
61
|
|
|
$andSelf |
62
|
|
|
) { |
63
|
170 |
|
$this->inverse = $inverse; |
64
|
170 |
|
$this->morphType = $name.'_type'; |
65
|
170 |
|
$this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass(); |
66
|
|
|
|
67
|
170 |
|
parent::__construct( |
68
|
170 |
|
$query, |
69
|
170 |
|
$parent, |
70
|
170 |
|
$table, |
71
|
170 |
|
$foreignPivotKey, |
72
|
170 |
|
$relatedPivotKey, |
73
|
170 |
|
$parentKey, |
74
|
170 |
|
$relatedKey, |
75
|
170 |
|
$andSelf |
76
|
170 |
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the where clause on the recursive expression query. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
106 |
|
protected function addExpressionWhereConstraints(Builder $query) |
86
|
|
|
{ |
87
|
106 |
|
parent::addExpressionWhereConstraints($query); |
88
|
|
|
|
89
|
106 |
|
$this->query->where( |
90
|
106 |
|
"$this->table.$this->morphType", |
91
|
106 |
|
$this->morphClass |
92
|
106 |
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the where clause on the recursive expression query for an eager load of the relation. |
97
|
|
|
* |
98
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
99
|
|
|
* @param array $models |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
40 |
|
public function addEagerExpressionWhereConstraints(Builder $query, array $models) |
103
|
|
|
{ |
104
|
40 |
|
parent::addEagerExpressionWhereConstraints($query, $models); |
105
|
|
|
|
106
|
40 |
|
$this->query->where( |
107
|
40 |
|
"$this->table.$this->morphType", |
108
|
40 |
|
$this->morphClass |
109
|
40 |
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set the where clause on the recursive expression query for an existence query. |
114
|
|
|
* |
115
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
116
|
|
|
* @param string $table |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
24 |
|
public function addExistenceExpressionWhereConstraints(Builder $query, $table) |
120
|
|
|
{ |
121
|
24 |
|
parent::addExistenceExpressionWhereConstraints($query, $table); |
122
|
|
|
|
123
|
24 |
|
$this->query->where( |
124
|
24 |
|
"$this->table.$this->morphType", |
125
|
24 |
|
$this->morphClass |
126
|
24 |
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|