1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Volosyuk\SimpleEloquent\Relations; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use stdClass; |
9
|
|
|
use Volosyuk\SimpleEloquent\Builder; |
10
|
|
|
use Volosyuk\SimpleEloquent\ModelAccessor; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait SimpleRelation |
14
|
|
|
* @package Volosyuk\SimpleEloquent |
15
|
|
|
* |
16
|
|
|
* @property Builder $query |
17
|
|
|
* @property Model $parent |
18
|
|
|
*/ |
19
|
|
|
trait Relation |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param $models |
23
|
|
|
* @param $name |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
9 |
|
public function eagerLoadAndMatchSimple($models, $name) |
27
|
|
|
{ |
28
|
9 |
|
$results = $this->getEagerSimple(); |
29
|
|
|
|
30
|
9 |
|
return $this->matchSimple($models, $results, $name); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $models |
35
|
|
|
* @param $relation |
36
|
|
|
* @return array|stdClass[] |
37
|
|
|
*/ |
38
|
10 |
|
public function initSimpleRelation(array &$models, $relation) |
39
|
|
|
{ |
40
|
10 |
|
foreach ($models as &$model) { |
41
|
10 |
|
ModelAccessor::set($model, $relation, null); |
42
|
|
|
} |
43
|
10 |
|
unset($model); |
44
|
|
|
|
45
|
10 |
|
return $models; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the relationship for eager loading. |
50
|
|
|
* |
51
|
|
|
* @return Collection |
52
|
|
|
*/ |
53
|
9 |
|
protected function getEagerSimple() |
54
|
|
|
{ |
55
|
9 |
|
return $this->getSimple(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get all of the primary keys for an array of models. |
60
|
|
|
* |
61
|
|
|
* @param array $models |
62
|
|
|
* @param string $key |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
protected function getKeys(array $models, $key = null) |
66
|
|
|
{ |
67
|
6 |
|
return array_unique(array_values(array_map(function ($value) use ($key) { |
68
|
6 |
|
return $key ? ModelAccessor::get($value, $key) : ModelAccessor::get($value, $this->parent->getKeyName()); |
69
|
6 |
|
}, $models))); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set the constraints for an eager load of the relation. |
74
|
|
|
* |
75
|
|
|
* @param array $models |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
4 |
|
public function addEagerConstraintsSimple(array $models) |
79
|
|
|
{ |
80
|
4 |
|
$this->query->whereIn($this->getQualifiedForeignKeyName(), $this->getKeys($models)); |
|
|
|
|
81
|
4 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Builds a table-keyed array from model class names. |
85
|
|
|
* |
86
|
|
|
* @param string[]|null $models |
87
|
|
|
* @return array|null |
88
|
|
|
*/ |
89
|
|
|
protected static function buildMorphMapFromModels(array $models = null) |
90
|
|
|
{ |
91
|
|
|
if (is_null($models) || Arr::isAssoc($models)) { |
92
|
|
|
return $models; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return array_combine(array_map(function ($model) { |
96
|
|
|
return (new $model)->getTable(); |
97
|
|
|
}, $models), $models); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|