1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Relations; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use MongoDB\Driver\Query; |
7
|
|
|
use Nip\Records\AbstractModels\Record; |
8
|
|
|
use Nip\Records\Collections\Collection; |
9
|
|
|
use Nip\Records\Relations\Exceptions\ModelNotLoadedInRelation; |
10
|
|
|
use Nip\Records\Relations\Traits\HasMorphTypeTrait; |
11
|
|
|
use Nip\HelperBroker; |
12
|
|
|
use Nip_Helper_Arrays as ArraysHelper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class MorphToMany |
16
|
|
|
* @package Nip\Records\Relations |
17
|
|
|
*/ |
18
|
|
|
class MorphTo extends BelongsTo |
19
|
|
|
{ |
20
|
|
|
use HasMorphTypeTrait; |
21
|
|
|
|
22
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
23
|
|
|
* @return string |
24
|
|
|
* @throws ModelNotLoadedInRelation |
25
|
|
|
*/ |
26
|
3 |
|
public function getWithClass() |
27
|
|
|
{ |
28
|
3 |
|
$type = $this->getMorphType(); |
29
|
2 |
|
$typePlural = inflector()->pluralize($type); |
30
|
2 |
|
return $typePlural; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return mixed |
35
|
|
|
* @throws ModelNotLoadedInRelation |
36
|
|
|
*/ |
37
|
3 |
|
public function getMorphType() |
38
|
|
|
{ |
39
|
3 |
|
if ($this->getItem() instanceof Record) { |
40
|
2 |
|
return $this->getItem()->{$this->getMorphTypeField()}; |
41
|
|
|
} |
42
|
1 |
|
throw new ModelNotLoadedInRelation( |
43
|
1 |
|
$this->debugString() |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $params |
49
|
|
|
* @throws Exception |
50
|
|
|
*/ |
51
|
1 |
|
public function addParams($params) |
52
|
|
|
{ |
53
|
1 |
|
$this->checkParamMorphPrefix($params); |
54
|
1 |
|
parent::addParams($params); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function getEagerResults($collection) |
61
|
|
|
{ |
62
|
|
|
if ($collection->count() < 1) { |
63
|
|
|
return $this->getWith()->newCollection(); |
64
|
|
|
} |
65
|
|
|
$types = $this->getTypesFromCollection($collection); |
66
|
|
|
$collection = new Collection(); |
67
|
|
|
foreach ($types as $type) { |
|
|
|
|
68
|
|
|
$manager = $this->getModelManagerInstance($type); |
69
|
|
|
$query = $this->getEagerQueryType($collection, $manager); |
70
|
|
|
$typeCollection = $manager->findByQuery($query); |
71
|
|
|
foreach ($typeCollection as $item) { |
72
|
|
|
$collection->add($item); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $collection; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Collection $collection |
81
|
|
|
* @param $manager |
82
|
|
|
* @return Query |
83
|
|
|
* @throws Exception |
84
|
|
|
*/ |
85
|
1 |
|
public function getEagerQueryType(Collection $collection, $manager) |
86
|
|
|
{ |
87
|
1 |
|
$fkList = $this->getEagerFkList($collection); |
88
|
1 |
|
$query = $manager->newQuery(); |
89
|
1 |
|
$query->where($manager->getPrimaryKey() . ' IN ?', $fkList); |
90
|
1 |
|
return $query; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Query $query |
95
|
|
|
* @param array $fkList |
96
|
|
|
* @return Query |
97
|
|
|
* @throws Exception |
98
|
|
|
*/ |
99
|
|
|
protected function populateEagerQueryFromFkList($query, $fkList) |
100
|
|
|
{ |
101
|
|
|
|
102
|
|
|
return $query; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $collection |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getTypesFromCollection($collection) |
110
|
|
|
{ |
111
|
|
|
$type = $this->getMorphTypeField(); |
|
|
|
|
112
|
|
|
|
113
|
|
|
/** @var ArraysHelper $arrayHelper */ |
114
|
|
|
$arrayHelper = HelperBroker::get('Arrays'); |
115
|
|
|
return $arrayHelper->pluck($collection, $type); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.