|
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
|
|
|
* An array to map class names to their morph names in database. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
public static $morphMap = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $models |
|
30
|
|
|
* @param $name |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
9 |
|
public function eagerLoadAndMatchSimple($models, $name) |
|
34
|
|
|
{ |
|
35
|
9 |
|
$results = $this->getEagerSimple(); |
|
36
|
|
|
|
|
37
|
9 |
|
return $this->matchSimple($models, $results, $name); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $models |
|
42
|
|
|
* @param $relation |
|
43
|
|
|
* @return array|stdClass[] |
|
44
|
|
|
*/ |
|
45
|
10 |
|
public function initSimpleRelation(array &$models, $relation) |
|
46
|
|
|
{ |
|
47
|
10 |
|
foreach ($models as &$model) { |
|
48
|
10 |
|
ModelAccessor::set($model, $relation, null); |
|
49
|
|
|
} |
|
50
|
10 |
|
unset($model); |
|
51
|
|
|
|
|
52
|
10 |
|
return $models; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the relationship for eager loading. |
|
57
|
|
|
* |
|
58
|
|
|
* @return Collection |
|
59
|
|
|
*/ |
|
60
|
9 |
|
protected function getEagerSimple() |
|
61
|
|
|
{ |
|
62
|
9 |
|
return $this->getSimple(); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get all of the primary keys for an array of models. |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $models |
|
69
|
|
|
* @param string $key |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getKeys(array $models, $key = null) |
|
73
|
|
|
{ |
|
74
|
6 |
|
return array_unique(array_values(array_map(function ($value) use ($key) { |
|
75
|
6 |
|
return $key ? ModelAccessor::get($value, $key) : ModelAccessor::get($value, $this->parent->getKeyName()); |
|
76
|
6 |
|
}, $models))); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set the constraints for an eager load of the relation. |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $models |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function addEagerConstraintsSimple(array $models) |
|
86
|
|
|
{ |
|
87
|
4 |
|
$this->query->whereIn($this->getQualifiedForeignKeyName(), $this->getKeys($models)); |
|
|
|
|
|
|
88
|
4 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set or get the morph map for polymorphic relations. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array|null $map |
|
95
|
|
|
* @param bool $merge |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public static function morphMap(array $map = null, $merge = true) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$map = static::buildMorphMapFromModels($map); |
|
101
|
|
|
|
|
102
|
1 |
|
if (is_array($map)) { |
|
103
|
|
|
static::$morphMap = $merge && static::$morphMap |
|
|
|
|
|
|
104
|
|
|
? $map + static::$morphMap : $map; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return static::$morphMap; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Builds a table-keyed array from model class names. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string[]|null $models |
|
115
|
|
|
* @return array|null |
|
116
|
|
|
*/ |
|
117
|
1 |
|
protected static function buildMorphMapFromModels(array $models = null) |
|
118
|
|
|
{ |
|
119
|
1 |
|
if (is_null($models) || Arr::isAssoc($models)) { |
|
120
|
1 |
|
return $models; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return array_combine(array_map(function ($model) { |
|
124
|
|
|
return (new $model)->getTable(); |
|
125
|
|
|
}, $models), $models); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|