|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Volosyuk\SimpleEloquent\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Volosyuk\SimpleEloquent\Builder; |
|
8
|
|
|
use Volosyuk\SimpleEloquent\ModelAccessor; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class MorphToWithSimple |
|
12
|
|
|
* @package Volosyuk\SimpleEloquent |
|
13
|
|
|
*/ |
|
14
|
|
|
class MorphTo extends \Illuminate\Database\Eloquent\Relations\MorphTo |
|
15
|
|
|
{ |
|
16
|
|
|
use Relation; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Get the results of the relationship. |
|
20
|
|
|
* |
|
21
|
|
|
* Called via eager load method of Eloquent query builder. |
|
22
|
|
|
* |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
*/ |
|
25
|
1 |
|
protected function getEagerSimple() |
|
26
|
|
|
{ |
|
27
|
1 |
|
foreach (array_keys($this->dictionary) as $type) { |
|
28
|
1 |
|
$this->matchSimpleToMorphParents($type, $this->getSimpleResultsByType($type)); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
return $this->models; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $models |
|
36
|
|
|
* @param $name |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function eagerLoadAndMatchSimple($models, $name) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->addEagerConstraintsSimple($models); |
|
42
|
|
|
|
|
43
|
1 |
|
return $this->getEagerSimple(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get all of the relation results for a type. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $type |
|
50
|
|
|
* @return Collection |
|
51
|
|
|
*/ |
|
52
|
1 |
|
protected function getSimpleResultsByType($type) |
|
53
|
|
|
{ |
|
54
|
|
|
/** |
|
55
|
|
|
* @var Model $instance |
|
56
|
|
|
*/ |
|
57
|
1 |
|
$instance = $this->createModelByType($type); |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var Builder $query |
|
61
|
|
|
*/ |
|
62
|
1 |
|
$query = $this->replayMacros($instance->newQuery()) |
|
63
|
1 |
|
->mergeConstraintsFrom($this->getQuery()) |
|
64
|
1 |
|
->with($this->getQuery()->getEagerLoads()); |
|
65
|
|
|
|
|
66
|
1 |
|
return $query->whereIn( |
|
|
|
|
|
|
67
|
1 |
|
$instance->getTable().'.'.$instance->getKeyName(), $this->gatherKeysByType($type) |
|
68
|
1 |
|
)->getSimple(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Match the results for a given type to their parents. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $type |
|
75
|
|
|
* @param Collection $results |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
1 |
|
protected function matchSimpleToMorphParents($type, Collection $results) |
|
79
|
|
|
{ |
|
80
|
1 |
|
foreach ($results as $result) { |
|
81
|
1 |
|
foreach ($this->models as &$model) { |
|
82
|
|
|
if ( |
|
83
|
1 |
|
ModelAccessor::get($model, $this->morphType) == $type |
|
84
|
|
|
&& |
|
85
|
1 |
|
ModelAccessor::get($model, $this->foreignKey) == ModelAccessor::get($result, $this->parent->getKeyName()) |
|
86
|
|
|
) { |
|
87
|
1 |
|
ModelAccessor::set($model, $this->relation, $result); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
1 |
|
unset($model); |
|
91
|
|
|
} |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set the constraints for an eager load of the relation. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $models |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function addEagerConstraintsSimple(array $models) |
|
102
|
|
|
{ |
|
103
|
1 |
|
$this->buildDictionarySimple($this->models = $models); |
|
|
|
|
|
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Build a dictionary with the models. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $models |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
1 |
|
protected function buildDictionarySimple(array $models) |
|
114
|
|
|
{ |
|
115
|
1 |
|
foreach ($models as $model) { |
|
116
|
1 |
|
if (ModelAccessor::get($model, $this->morphType)) { |
|
117
|
1 |
|
$this->dictionary[ModelAccessor::get($model, $this->morphType)][ModelAccessor::get($model, $this->foreignKey)][] = $model; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Gather all of the foreign keys for a given type. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $type |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function gatherSimpleKeysByType($type) |
|
129
|
|
|
{ |
|
130
|
|
|
$foreign = $this->foreignKey; |
|
131
|
|
|
|
|
132
|
|
|
return collect($this->dictionary[$type])->map(function ($models) use ($foreign) { |
|
|
|
|
|
|
133
|
|
|
return ModelAccessor::get(head($models), $foreign); |
|
134
|
|
|
})->values()->unique(); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.