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