1
|
|
|
<?php namespace Arcanedev\LaravelNestedSet\Eloquent; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelNestedSet\Utilities\NestedSet; |
4
|
|
|
use Arcanedev\LaravelNestedSet\NodeTrait; |
5
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
9
|
|
|
use Illuminate\Database\Query\Builder; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class DescendantsRelation |
14
|
|
|
* |
15
|
|
|
* @package Arcanedev\LaravelNestedSet\Eloquent |
16
|
|
|
* @author ARCANEDEV <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @method static \Arcanedev\LaravelNestedSet\Eloquent\Collection get(array $columns = ['*']) |
19
|
|
|
*/ |
20
|
|
|
class DescendantsRelation extends Relation |
21
|
|
|
{ |
22
|
|
|
/* ----------------------------------------------------------------- |
23
|
|
|
| Properties |
24
|
|
|
| ----------------------------------------------------------------- |
25
|
|
|
*/ |
26
|
|
|
/** |
27
|
|
|
* The Eloquent query builder instance. |
28
|
|
|
* |
29
|
|
|
* @var \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder |
30
|
|
|
*/ |
31
|
|
|
protected $query; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The parent model instance. |
35
|
|
|
* |
36
|
|
|
* @var \Arcanedev\LaravelNestedSet\NodeTrait |
37
|
|
|
*/ |
38
|
|
|
protected $parent; |
39
|
|
|
|
40
|
|
|
/* ----------------------------------------------------------------- |
41
|
|
|
| Constructor |
42
|
|
|
| ----------------------------------------------------------------- |
43
|
|
|
*/ |
44
|
|
|
/** |
45
|
|
|
* DescendantsRelation constructor. |
46
|
|
|
* |
47
|
|
|
* @param \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder $builder |
48
|
|
|
* @param \Illuminate\Database\Eloquent\Model|NodeTrait $model |
49
|
|
|
*/ |
50
|
36 |
|
public function __construct(QueryBuilder $builder, Model $model) |
51
|
|
|
{ |
52
|
|
|
// @codeCoverageIgnoreStart |
53
|
|
|
if ( ! NestedSet::isNode($model)) { |
54
|
|
|
throw new InvalidArgumentException('Model must be node.'); |
55
|
|
|
} |
56
|
|
|
// @codeCoverageIgnoreEnd |
57
|
|
|
|
58
|
36 |
|
parent::__construct($builder, $model); |
59
|
36 |
|
} |
60
|
|
|
|
61
|
|
|
/* ----------------------------------------------------------------- |
62
|
|
|
| Main Methods |
63
|
|
|
| ----------------------------------------------------------------- |
64
|
|
|
*/ |
65
|
|
|
/** |
66
|
|
|
* Add the constraints for a relationship query. |
67
|
|
|
* |
68
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
69
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parent |
70
|
|
|
* @param array|mixed $columns |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
73
|
|
|
*/ |
74
|
3 |
|
public function getRelationExistenceQuery( |
75
|
|
|
EloquentBuilder $query, |
76
|
|
|
EloquentBuilder $parent, |
77
|
|
|
$columns = ['*'] |
78
|
|
|
) { |
79
|
3 |
|
$query->select($columns); |
|
|
|
|
80
|
|
|
|
81
|
3 |
|
$table = $query->getModel()->getTable(); |
82
|
3 |
|
$hash = $this->getRelationCountHash(); |
83
|
|
|
|
84
|
3 |
|
$query->from("$table as $hash"); |
85
|
|
|
|
86
|
3 |
|
$grammar = $query->getQuery()->getGrammar(); |
87
|
|
|
|
88
|
3 |
|
$table = $grammar->wrapTable($table); |
89
|
3 |
|
$hash = $grammar->wrapTable($hash); |
90
|
3 |
|
$lft = $grammar->wrap($this->parent->getLftName()); |
91
|
3 |
|
$rgt = $grammar->wrap($this->parent->getRgtName()); |
92
|
|
|
|
93
|
3 |
|
return $query->whereRaw("{$hash}.{$lft} between {$table}.{$lft} + 1 and {$table}.{$rgt}"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
98
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $parent |
99
|
|
|
* @param array|mixed $columns |
100
|
|
|
* |
101
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
102
|
|
|
*/ |
103
|
|
|
public function getRelationQuery( |
104
|
|
|
EloquentBuilder $query, |
105
|
|
|
EloquentBuilder $parent, |
106
|
|
|
$columns = ['*'] |
107
|
|
|
) { |
108
|
|
|
return $this->getRelationExistenceQuery($query, $parent, $columns); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get a relationship join table hash. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
3 |
|
public function getRelationCountHash() |
117
|
|
|
{ |
118
|
3 |
|
return 'self_'.md5(microtime(true)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set the base constraints on the relation query. |
123
|
|
|
*/ |
124
|
36 |
|
public function addConstraints() |
125
|
|
|
{ |
126
|
36 |
|
if ( ! static::$constraints) return; |
127
|
|
|
|
128
|
30 |
|
$this->query->whereDescendantOf($this->parent); |
129
|
30 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the constraints for an eager load of the relation. |
133
|
|
|
* |
134
|
|
|
* @param array $models |
135
|
|
|
*/ |
136
|
|
|
public function addEagerConstraints(array $models) |
137
|
|
|
{ |
138
|
3 |
|
$this->query->whereNested(function (Builder $inner) use ($models) { |
|
|
|
|
139
|
|
|
// We will use this query in order to apply constraints to the base query builder |
140
|
3 |
|
$outer = $this->parent->newQuery(); |
141
|
|
|
|
142
|
3 |
|
foreach ($models as $model) { |
143
|
3 |
|
$outer->setQuery($inner)->orWhereDescendantOf($model); |
144
|
1 |
|
} |
145
|
3 |
|
}); |
146
|
3 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Initialize the relation on a set of models. |
150
|
|
|
* |
151
|
|
|
* @param array $models |
152
|
|
|
* @param string $relation |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
3 |
|
public function initRelation(array $models, $relation) |
157
|
|
|
{ |
158
|
3 |
|
return $models; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Match the eagerly loaded results to their parents. |
163
|
|
|
* |
164
|
|
|
* @param array $models |
165
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
166
|
|
|
* @param string $relation |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
3 |
|
public function match(array $models, EloquentCollection $results, $relation) |
171
|
|
|
{ |
172
|
3 |
|
foreach ($models as $model) { |
173
|
|
|
/** @var \Illuminate\Database\Eloquent\Model $model */ |
174
|
3 |
|
$model->setRelation( |
175
|
3 |
|
$relation, $this->getDescendantsForModel($model, $results) |
176
|
1 |
|
); |
177
|
1 |
|
} |
178
|
|
|
|
179
|
3 |
|
return $models; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the results of the relationship. |
184
|
|
|
* |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
3 |
|
public function getResults() |
188
|
|
|
{ |
189
|
3 |
|
return $this->query->get(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
194
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
195
|
|
|
* |
196
|
|
|
* @return \Arcanedev\LaravelNestedSet\Eloquent\Collection|\Illuminate\Database\Eloquent\Collection |
197
|
|
|
*/ |
198
|
3 |
|
protected function getDescendantsForModel(Model $model, EloquentCollection $results) |
199
|
|
|
{ |
200
|
3 |
|
$result = $this->related->newCollection(); |
201
|
|
|
|
202
|
3 |
|
foreach ($results as $descendant) { |
203
|
3 |
|
if ($descendant->isDescendantOf($model)) { |
204
|
3 |
|
$result->push($descendant); |
205
|
1 |
|
} |
206
|
1 |
|
} |
207
|
|
|
|
208
|
3 |
|
return $result; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.