-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathBaseRelation.php
205 lines (176 loc) · 4.89 KB
/
BaseRelation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Kalnoy\Nestedset;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder;
use InvalidArgumentException;
abstract class BaseRelation extends Relation
{
/**
* @var QueryBuilder
*/
protected $query;
/**
* @var NodeTrait|Model
*/
protected $parent;
/**
* The count of self joins.
*
* @var int
*/
protected static $selfJoinCount = 0;
/**
* AncestorsRelation constructor.
*
* @param QueryBuilder $builder
* @param Model $model
*/
public function __construct(QueryBuilder $builder, Model $model)
{
if ( ! NestedSet::isNode($model)) {
throw new InvalidArgumentException('Model must be node.');
}
parent::__construct($builder, $model);
}
/**
* @param Model $model
* @param $related
*
* @return bool
*/
abstract protected function matches(Model $model, $related);
/**
* @param QueryBuilder $query
* @param Model $model
*
* @return void
*/
abstract protected function addEagerConstraint($query, $model);
/**
* @param $hash
* @param $table
* @param $lft
* @param $rgt
*
* @return string
*/
abstract protected function relationExistenceCondition($hash, $table, $lft, $rgt);
/**
* @param EloquentBuilder $query
* @param EloquentBuilder $parent
* @param array $columns
*
* @return mixed
*/
public function getRelationExistenceQuery(EloquentBuilder $query, EloquentBuilder $parent,
$columns = [ '*' ]
) {
$query = $this->getParent()->replicate()->newScopedQuery()->select($columns);
$table = $query->getModel()->getTable();
$query->from($table.' as '.$hash = $this->getRelationCountHash());
$query->getModel()->setTable($hash);
$grammar = $query->getQuery()->getGrammar();
$condition = $this->relationExistenceCondition(
$grammar->wrapTable($hash),
$grammar->wrapTable($table),
$grammar->wrap($this->parent->getLftName()),
$grammar->wrap($this->parent->getRgtName()));
return $query->whereRaw($condition);
}
/**
* Initialize the relation on a set of models.
*
* @param array $models
* @param string $relation
*
* @return array
*/
public function initRelation(array $models, $relation)
{
return $models;
}
/**
* Get a relationship join table hash.
*
* @param bool $incrementJoinCount
* @return string
*/
public function getRelationCountHash($incrementJoinCount = true)
{
return 'nested_set_'.($incrementJoinCount ? static::$selfJoinCount++ : static::$selfJoinCount);
}
/**
* Get the results of the relationship.
*
* @return mixed
*/
public function getResults()
{
return $this->query->get();
}
/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
*
* @return void
*/
public function addEagerConstraints(array $models)
{
$this->query->whereNested(function (Builder $inner) use ($models) {
// We will use this query in order to apply constraints to the
// base query builder
$outer = $this->parent->newQuery()->setQuery($inner);
foreach ($models as $model) {
$this->addEagerConstraint($outer, $model);
}
});
}
/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param EloquentCollection $results
* @param string $relation
*
* @return array
*/
public function match(array $models, EloquentCollection $results, $relation)
{
foreach ($models as $model) {
$related = $this->matchForModel($model, $results);
$model->setRelation($relation, $related);
}
return $models;
}
/**
* @param Model $model
* @param EloquentCollection $results
*
* @return Collection
*/
protected function matchForModel(Model $model, EloquentCollection $results)
{
$result = $this->related->newCollection();
foreach ($results as $related) {
if ($this->matches($model, $related)) {
$result->push($related);
}
}
return $result;
}
/**
* Get the plain foreign key.
*
* @return mixed
*/
public function getForeignKeyName()
{
// Return a stub value for relation
// resolvers which need this function.
return NestedSet::PARENT_ID;
}
}