|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Query\Expression; |
|
9
|
|
|
|
|
10
|
|
|
trait IsRecursiveRelation |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Whether to include the parent model. |
|
14
|
|
|
* |
|
15
|
|
|
* @var bool |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $andSelf; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create a new recursive relationship instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
23
|
|
|
* @param \Illuminate\Database\Eloquent\Model $parent |
|
24
|
|
|
* @param string $foreignKey |
|
25
|
|
|
* @param string $localKey |
|
26
|
|
|
* @param bool $andSelf |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
231 |
|
public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf) |
|
30
|
|
|
{ |
|
31
|
231 |
|
$this->andSelf = $andSelf; |
|
32
|
|
|
|
|
33
|
231 |
|
parent::__construct($query, $parent, $foreignKey, $localKey); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Build model dictionary. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $results |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
50 |
|
protected function buildDictionary(Collection $results) |
|
43
|
|
|
{ |
|
44
|
50 |
|
return $results->mapToDictionary(function (Model $result) { |
|
45
|
50 |
|
return [$result->getFirstPathSegment() => $result]; |
|
46
|
50 |
|
})->all(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the fully qualified local key name. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
149 |
|
public function getQualifiedLocalKeyName() |
|
55
|
|
|
{ |
|
56
|
149 |
|
return $this->qualifyColumn($this->localKey); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Handle dynamic method calls to the relationship. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $method |
|
63
|
|
|
* @param array $parameters |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
191 |
|
public function __call($method, $parameters) |
|
67
|
|
|
{ |
|
68
|
191 |
|
$methods = ['update', 'increment', 'decrement', 'delete', 'forceDelete']; |
|
69
|
|
|
|
|
70
|
191 |
|
if (in_array($method, $methods)) { |
|
71
|
24 |
|
$expression = $this->query->getQuery()->from; |
|
72
|
|
|
|
|
73
|
24 |
|
$table = $this->parent->getTable(); |
|
74
|
|
|
|
|
75
|
24 |
|
$this->query->getQuery()->from = $table; |
|
76
|
|
|
|
|
77
|
24 |
|
$this->query->getModel()->setTable($table); |
|
78
|
|
|
|
|
79
|
24 |
|
$keys = $this->query->getQuery()->newQuery()->from($expression)->select($this->localKey); |
|
80
|
|
|
|
|
81
|
24 |
|
return $this->query->whereIn($this->getQualifiedLocalKeyName(), $keys)->$method(...$parameters); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
191 |
|
return parent::__call($method, $parameters); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Replace table hash with expression name in self-relation aggregate queries. |
|
89
|
|
|
* |
|
90
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
91
|
|
|
* @param \Illuminate\Database\Query\Expression $expression |
|
92
|
|
|
* @return \Illuminate\Database\Query\Expression |
|
93
|
|
|
*/ |
|
94
|
21 |
|
protected function replaceTableHash(Builder $query, Expression $expression) |
|
95
|
|
|
{ |
|
96
|
21 |
|
return new Expression( |
|
97
|
21 |
|
str_replace( |
|
98
|
21 |
|
$query->getGrammar()->wrap( |
|
|
|
|
|
|
99
|
21 |
|
$this->getRelationCountHash(false) |
|
|
|
|
|
|
100
|
|
|
), |
|
101
|
21 |
|
$query->getGrammar()->wrap( |
|
|
|
|
|
|
102
|
21 |
|
$query->getModel()->getExpressionName() |
|
103
|
|
|
), |
|
104
|
21 |
|
$expression->getValue(), |
|
105
|
|
|
) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|