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