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; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param Record $item |
||
51 | */ |
||
52 | 1 | public function saveResult(Record $item) |
|
53 | { |
||
54 | 1 | $primaryKey = $this->getPrimaryKey(); |
|
55 | 1 | $foreignKey = $this->getFK(); |
|
56 | |||
57 | 1 | $item->{$foreignKey} = $this->getItem()->{$primaryKey}; |
|
58 | 1 | $item->saveRecord(); |
|
59 | 1 | } |
|
60 | |||
61 | /** |
||
62 | * @throws \Exception |
||
63 | */ |
||
64 | 1 | public function initResults() |
|
65 | { |
||
66 | 1 | $collection = $this->newCollection(); |
|
67 | 1 | if ($this->isPopulatable()) { |
|
68 | $query = $this->getQuery(); |
||
69 | $items = $this->getWith()->findByQuery($query); |
||
70 | $this->populateCollection($collection, $items); |
||
71 | } |
||
72 | 1 | $this->setResults($collection); |
|
73 | 1 | } |
|
74 | |||
75 | /** |
||
76 | * @param RecordCollection $collection |
||
77 | * @param Collection $items |
||
78 | */ |
||
79 | public function populateCollection(RecordCollection $collection, $items) |
||
80 | { |
||
81 | foreach ($items as $item) { |
||
82 | $collection->add($item); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** @noinspection PhpMissingParentCallCommonInspection |
||
87 | * @inheritdoc |
||
88 | */ |
||
89 | 1 | public function populateEagerQueryFromFkList($query, $fkList) |
|
90 | { |
||
91 | 1 | $query->where($this->getFK() . ' IN ?', $fkList); |
|
92 | 1 | return $query; |
|
93 | } |
||
94 | |||
95 | /** @noinspection PhpMissingParentCallCommonInspection |
||
96 | * @param RecordCollection $collection |
||
97 | * @return array |
||
98 | */ |
||
99 | 1 | public function getEagerFkList(RecordCollection $collection) |
|
100 | { |
||
101 | 1 | if ($collection->isEmpty()) { |
|
102 | 1 | return []; |
|
103 | } |
||
104 | 1 | $key = $this->getPrimaryKey(); |
|
105 | /** @var ArraysHelper $arrayHelper */ |
||
106 | 1 | $arrayHelper = HelperBroker::get('Arrays'); |
|
107 | 1 | $return = $arrayHelper->pluck($collection, $key); |
|
108 | |||
109 | 1 | return array_unique($return); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param array $dictionary |
||
114 | * @param Collection $collection |
||
115 | * @param Record $record |
||
116 | * @return AssociatedCollection |
||
117 | */ |
||
118 | public function getResultsFromCollectionDictionary($dictionary, $collection, $record) |
||
119 | { |
||
120 | /** Use Relation Primary Key in case it is overwritten */ |
||
121 | $foreignKey = $this->getPrimaryKey(); |
||
122 | $primaryKey = $record->{$foreignKey}; |
||
123 | $collection = $this->newCollection(); |
||
124 | |||
125 | if (isset($dictionary[$primaryKey]) && is_array($dictionary[$primaryKey])) { |
||
126 | foreach ($dictionary[$primaryKey] as $record) { |
||
0 ignored issues
–
show
|
|||
127 | $collection->add($record); |
||
128 | } |
||
129 | } |
||
130 | return $collection; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Build model dictionary keyed by the relation's foreign key. |
||
135 | * |
||
136 | * @param RecordCollection $collection |
||
137 | * @return array |
||
138 | */ |
||
139 | protected function buildDictionary(RecordCollection $collection) |
||
140 | { |
||
141 | $dictionary = []; |
||
142 | $primaryKey = $this->getDictionaryKey(); |
||
143 | foreach ($collection as $record) { |
||
144 | $dictionary[$record->{$primaryKey}][] = $record; |
||
145 | } |
||
146 | return $dictionary; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | protected function getDictionaryKey() |
||
153 | { |
||
154 | return $this->getFK(); |
||
155 | } |
||
156 | } |
||
157 |