|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Eloquent\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Expression; |
|
6
|
|
|
use Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Eloquent\Relations\Traits\HasEagerLimit; |
|
7
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants as Base; |
|
8
|
|
|
|
|
9
|
|
|
class Descendants extends Base |
|
10
|
|
|
{ |
|
11
|
|
|
use HasEagerLimit; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Set the "limit" value of the query. |
|
15
|
|
|
* |
|
16
|
|
|
* @param int $value |
|
17
|
|
|
* @return $this |
|
18
|
|
|
*/ |
|
19
|
20 |
|
public function limit($value) |
|
20
|
|
|
{ |
|
21
|
20 |
|
if ($this->parent->exists) { |
|
22
|
4 |
|
$this->query->limit($value); |
|
23
|
|
|
} else { |
|
24
|
16 |
|
if ($this->andSelf) { |
|
25
|
8 |
|
$this->addGroupLimit($value); |
|
26
|
|
|
} else { |
|
27
|
8 |
|
$grammar = $this->getEagerLimitGrammar(); |
|
28
|
|
|
|
|
29
|
8 |
|
$firstPathSegment = $grammar->compileFirstPathSegment( |
|
30
|
8 |
|
$this->related->qualifyColumn( |
|
31
|
8 |
|
$this->related->getPathName() |
|
|
|
|
|
|
32
|
8 |
|
) |
|
33
|
8 |
|
); |
|
34
|
|
|
|
|
35
|
8 |
|
$parentKeyQuery = $this->related->newModelQuery() |
|
36
|
8 |
|
->select($this->getForeignKeyName()) |
|
37
|
8 |
|
->from($this->related->getTable(), 'laravel_alias') |
|
38
|
8 |
|
->where($this->localKey, new Expression($firstPathSegment)) |
|
39
|
8 |
|
->limit(1); |
|
40
|
|
|
|
|
41
|
8 |
|
$sql = $grammar->compileParentKeyOfFirstPathSegment( |
|
42
|
8 |
|
$this->related->qualifyColumn( |
|
43
|
8 |
|
$this->related->getPathName() |
|
44
|
8 |
|
), |
|
45
|
8 |
|
$this->related->qualifyColumn( |
|
46
|
8 |
|
$this->related->getParentKeyName() |
|
47
|
8 |
|
), |
|
48
|
8 |
|
$parentKeyQuery->getQuery() |
|
49
|
8 |
|
); |
|
50
|
|
|
|
|
51
|
8 |
|
$column = new Expression($sql); |
|
52
|
|
|
|
|
53
|
8 |
|
$this->query->groupLimit($value, $column); |
|
54
|
|
|
|
|
55
|
8 |
|
$this->query->getQuery()->addBinding( |
|
56
|
8 |
|
array_fill( |
|
57
|
8 |
|
0, |
|
58
|
8 |
|
substr_count($sql, '?'), |
|
59
|
8 |
|
$this->related->getPathSeparator() |
|
60
|
8 |
|
), |
|
61
|
8 |
|
'select' |
|
62
|
8 |
|
); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
20 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|