1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Relations; |
4
|
|
|
|
5
|
|
|
use Nip\Database\Query\Select as Query; |
6
|
|
|
use Nip\HelperBroker; |
7
|
|
|
use Nip_Helper_Arrays as ArraysHelper; |
8
|
|
|
use Nip\Records\AbstractModels\Record as Record; |
9
|
|
|
use Nip\Records\Collections\Associated as AssociatedCollection; |
10
|
|
|
use Nip\Records\Collections\Collection; |
11
|
|
|
use Nip\Records\Collections\Collection as RecordCollection; |
12
|
|
|
use Nip\Records\Relations\Traits\HasCollectionResults; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class HasOneOrMany |
16
|
|
|
* @package Nip\Records\Relations |
17
|
|
|
*/ |
18
|
|
|
abstract class HasOneOrMany extends Relation |
19
|
|
|
{ |
20
|
|
|
use HasCollectionResults; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $type = 'hasMany'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
public function save() |
31
|
|
|
{ |
32
|
|
|
if ($this->hasResults()) { |
33
|
|
|
$collection = $this->getResults(); |
34
|
|
|
foreach ($collection as $item) { |
|
|
|
|
35
|
|
|
$this->saveResult($item); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function hasResults() |
45
|
|
|
{ |
46
|
|
|
return $this->isPopulated() && count($this->getResults()) > 0; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Record $item |
51
|
|
|
*/ |
52
|
1 |
|
public function saveResult(Record $item) |
53
|
|
|
{ |
54
|
1 |
|
$primaryKey = $this->getManager()->getPrimaryKey(); |
55
|
1 |
|
$foreignKey = $this->getFK(); |
56
|
1 |
|
$item->{$foreignKey} = $this->getItem()->{$primaryKey}; |
57
|
1 |
|
$item->saveRecord(); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
1 |
|
public function initResults() |
64
|
|
|
{ |
65
|
1 |
|
$collection = $this->newCollection(); |
66
|
1 |
|
if ($this->isPopulatable()) { |
67
|
|
|
$query = $this->getQuery(); |
68
|
|
|
$items = $this->getWith()->findByQuery($query); |
69
|
|
|
$this->populateCollection($collection, $items); |
70
|
|
|
} |
71
|
1 |
|
$this->setResults($collection); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param RecordCollection $collection |
76
|
|
|
* @param Collection $items |
77
|
|
|
*/ |
78
|
|
|
public function populateCollection(RecordCollection $collection, $items) |
79
|
|
|
{ |
80
|
|
|
foreach ($items as $item) { |
81
|
|
|
$collection->add($item); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
1 |
|
public function populateEagerQueryFromFkList($query, $fkList) |
89
|
|
|
{ |
90
|
1 |
|
$query->where($this->getFK() . ' IN ?', $fkList); |
91
|
1 |
|
return $query; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
95
|
|
|
* @param RecordCollection $collection |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
1 |
|
public function getEagerFkList(RecordCollection $collection) |
99
|
|
|
{ |
100
|
1 |
|
if ($collection->isEmpty()) { |
101
|
1 |
|
return []; |
102
|
|
|
} |
103
|
1 |
|
$key = $collection->getManager()->getPrimaryKey(); |
104
|
|
|
/** @var ArraysHelper $arrayHelper */ |
105
|
1 |
|
$arrayHelper = HelperBroker::get('Arrays'); |
106
|
1 |
|
$return = $arrayHelper->pluck($collection, $key); |
107
|
|
|
|
108
|
1 |
|
return array_unique($return); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $dictionary |
113
|
|
|
* @param Collection $collection |
114
|
|
|
* @param Record $record |
115
|
|
|
* @return AssociatedCollection |
116
|
|
|
*/ |
117
|
|
|
public function getResultsFromCollectionDictionary($dictionary, $collection, $record) |
118
|
|
|
{ |
119
|
|
|
$foreignKey = $record->getManager()->getPrimaryKey(); |
120
|
|
|
$primaryKey = $record->{$foreignKey}; |
121
|
|
|
$collection = $this->newCollection(); |
122
|
|
|
|
123
|
|
|
if (isset($dictionary[$primaryKey])) { |
124
|
|
|
foreach ($dictionary[$primaryKey] as $record) { |
125
|
|
|
$collection->add($record); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return $collection; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Build model dictionary keyed by the relation's foreign key. |
133
|
|
|
* |
134
|
|
|
* @param RecordCollection $collection |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
protected function buildDictionary(RecordCollection $collection) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$dictionary = []; |
140
|
|
|
$primaryKey = $this->getDictionaryKey(); |
141
|
|
|
foreach ($collection as $record) { |
142
|
|
|
$dictionary[$record->{$primaryKey}][] = $record; |
143
|
|
|
} |
144
|
|
|
return $dictionary; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
protected function getDictionaryKey() |
151
|
|
|
{ |
152
|
|
|
return $this->getFK(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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.