@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | * Replaces DataList when EagerLoading is used. Fetches data when the main query is actually executed. |
11 | 11 | * Appends related objects when a DataObject is actually created. |
12 | 12 | */ |
13 | -class EagerLoadedDataList extends DataList{ |
|
13 | +class EagerLoadedDataList extends DataList { |
|
14 | 14 | |
15 | 15 | const ID_LIMIT = 5000; |
16 | 16 | public $withList = []; |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | |
23 | 23 | public function __construct($classOrList) |
24 | 24 | { |
25 | - if(is_string($classOrList)) { |
|
25 | + if (is_string($classOrList)) { |
|
26 | 26 | parent::__construct($classOrList); |
27 | 27 | } else { |
28 | 28 | parent::__construct($classOrList->dataClass()); |
@@ -40,10 +40,10 @@ discard block |
||
40 | 40 | return $clone; |
41 | 41 | } |
42 | 42 | |
43 | - public static function extractField($arr,$field) |
|
43 | + public static function extractField($arr, $field) |
|
44 | 44 | { |
45 | 45 | $result = []; |
46 | - foreach($arr as $record){ |
|
46 | + foreach ($arr as $record) { |
|
47 | 47 | $result[is_object($record) ? $record->ID : $record['ID']] = is_object($record) ? $record->$field : $record[$field]; |
48 | 48 | } |
49 | 49 | return $result; |
@@ -51,8 +51,8 @@ discard block |
||
51 | 51 | public static function reverseMap($arr) |
52 | 52 | { |
53 | 53 | $result = []; |
54 | - foreach($arr as $k => $v){ |
|
55 | - if(!isset($result[$v])) $result[$v] = []; |
|
54 | + foreach ($arr as $k => $v) { |
|
55 | + if (!isset($result[$v])) $result[$v] = []; |
|
56 | 56 | $result[$v][] = $k; |
57 | 57 | } |
58 | 58 | return $result; |
@@ -76,35 +76,35 @@ discard block |
||
76 | 76 | private $relationsPrepared = false; |
77 | 77 | |
78 | 78 | public function prepareEagerRelations() { |
79 | - if($this->relationsPrepared) return; |
|
79 | + if ($this->relationsPrepared) return; |
|
80 | 80 | $this->relationsPrepared = true; |
81 | 81 | $localClass = $this->dataClass(); |
82 | 82 | $config = Config::forClass($localClass); |
83 | - $hasOnes = (array)$config->get('has_one'); |
|
84 | - $hasManys = (array)$config->get('has_many'); |
|
85 | - $manyManys = (array)$config->get('many_many'); |
|
83 | + $hasOnes = (array) $config->get('has_one'); |
|
84 | + $hasManys = (array) $config->get('has_many'); |
|
85 | + $manyManys = (array) $config->get('many_many'); |
|
86 | 86 | |
87 | 87 | //collect has_ones |
88 | - $withHasOnes = array_filter($this->withList,function($dep)use($hasOnes){ return array_key_exists($dep[0],$hasOnes); }); |
|
89 | - $withHasManys = array_filter($this->withList,function($dep)use($hasManys){ return array_key_exists($dep[0],$hasManys); }); |
|
90 | - $withManyManys = array_filter($this->withList,function($dep)use($manyManys){ return array_key_exists($dep[0],$manyManys); }); |
|
88 | + $withHasOnes = array_filter($this->withList, function($dep)use($hasOnes){ return array_key_exists($dep[0], $hasOnes); }); |
|
89 | + $withHasManys = array_filter($this->withList, function($dep)use($hasManys){ return array_key_exists($dep[0], $hasManys); }); |
|
90 | + $withManyManys = array_filter($this->withList, function($dep)use($manyManys){ return array_key_exists($dep[0], $manyManys); }); |
|
91 | 91 | |
92 | - if(!count($withHasOnes) && !count($withHasManys) && !count($withManyManys)){ |
|
92 | + if (!count($withHasOnes) && !count($withHasManys) && !count($withManyManys)) { |
|
93 | 93 | // do nothing if no matches |
94 | 94 | /** @todo report errors */ |
95 | 95 | return; |
96 | 96 | } |
97 | 97 | |
98 | 98 | $data = $this->column('ID'); |
99 | - if(count($withHasOnes)){ |
|
99 | + if (count($withHasOnes)) { |
|
100 | 100 | $this->_prepareCache($hasOnes, $withHasOnes); |
101 | 101 | $this->eagerLoadHasOne($data, $hasOnes, $withHasOnes); |
102 | 102 | } |
103 | - if(count($withHasManys)){ |
|
103 | + if (count($withHasManys)) { |
|
104 | 104 | $this->_prepareCache($hasManys, $withHasManys); |
105 | 105 | $this->eagerLoadHasMany($data, $hasManys, $withHasManys); |
106 | 106 | } |
107 | - if(count($withManyManys)){ |
|
107 | + if (count($withManyManys)) { |
|
108 | 108 | $this->_prepareCache($manyManys, $withManyManys); |
109 | 109 | $this->eagerLoadManyMany($data, $manyManys, $withManyManys); |
110 | 110 | } |
@@ -117,15 +117,15 @@ discard block |
||
117 | 117 | |
118 | 118 | //collect required IDS |
119 | 119 | $fields = ['ID']; |
120 | - foreach($withHasOnes as $depSeq) { |
|
120 | + foreach ($withHasOnes as $depSeq) { |
|
121 | 121 | $dep = $depSeq[0]; |
122 | 122 | $fields[] = "{$dep}ID"; |
123 | 123 | } |
124 | 124 | $table = Config::forClass($this->dataClass)->get('table_name'); |
125 | - $data = new SQLSelect(implode(',',$fields),[$table],["ID IN (".implode(',',$ids).")"]); |
|
126 | - $data = self::EnsureArray($data->execute(),'ID'); |
|
125 | + $data = new SQLSelect(implode(',', $fields), [$table], ["ID IN (".implode(',', $ids).")"]); |
|
126 | + $data = self::EnsureArray($data->execute(), 'ID'); |
|
127 | 127 | |
128 | - foreach($withHasOnes as $depSeq) { |
|
128 | + foreach ($withHasOnes as $depSeq) { |
|
129 | 129 | $dep = $depSeq[0]; |
130 | 130 | $depClass = $hasOnes[$dep]; |
131 | 131 | |
@@ -137,17 +137,17 @@ discard block |
||
137 | 137 | |
138 | 138 | $component = $schema->hasOneComponent($this->dataClass, $dep); |
139 | 139 | |
140 | - $descriptor['map'] = self::extractField($data,$descriptor['localField']); |
|
140 | + $descriptor['map'] = self::extractField($data, $descriptor['localField']); |
|
141 | 141 | $uniqueIDs = array_unique($descriptor['map']); |
142 | - while(count($uniqueIDs)) { |
|
143 | - $IDsubset = array_splice($uniqueIDs,0,self::ID_LIMIT); |
|
144 | - $result = DataObject::get($depClass)->filter('ID',$IDsubset); |
|
145 | - if(count($depSeq)>1){ |
|
142 | + while (count($uniqueIDs)) { |
|
143 | + $IDsubset = array_splice($uniqueIDs, 0, self::ID_LIMIT); |
|
144 | + $result = DataObject::get($depClass)->filter('ID', $IDsubset); |
|
145 | + if (count($depSeq) > 1) { |
|
146 | 146 | $result = $result |
147 | - ->with(implode('.',array_slice($depSeq,1))); |
|
147 | + ->with(implode('.', array_slice($depSeq, 1))); |
|
148 | 148 | } |
149 | 149 | |
150 | - foreach($result as $depRecord) { |
|
150 | + foreach ($result as $depRecord) { |
|
151 | 151 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
152 | 152 | } |
153 | 153 | } |
@@ -160,9 +160,9 @@ discard block |
||
160 | 160 | public function eagerLoadHasMany($data, $hasManys, $withHasManys) |
161 | 161 | { |
162 | 162 | $localClass = $this->dataClass(); |
163 | - $localClassTail = basename(str_replace('\\','/',$localClass)); |
|
163 | + $localClassTail = basename(str_replace('\\', '/', $localClass)); |
|
164 | 164 | |
165 | - foreach($withHasManys as $depSeq) { |
|
165 | + foreach ($withHasManys as $depSeq) { |
|
166 | 166 | $dep = $depSeq[0]; |
167 | 167 | $depClass = $hasManys[$dep]; |
168 | 168 | $localNameInDep = $localClassTail; |
@@ -173,18 +173,18 @@ discard block |
||
173 | 173 | 'remoteField' => $depKey, |
174 | 174 | 'map' => [], |
175 | 175 | ]; |
176 | - $result = DataObject::get($depClass)->filter($depKey,$data); |
|
177 | - if(count($depSeq)>1){ |
|
176 | + $result = DataObject::get($depClass)->filter($depKey, $data); |
|
177 | + if (count($depSeq) > 1) { |
|
178 | 178 | $result = $result |
179 | - ->with(implode('.',array_slice($depSeq,1))); |
|
179 | + ->with(implode('.', array_slice($depSeq, 1))); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | $collection = []; |
183 | 183 | |
184 | - foreach($data as $localRecordID){ |
|
184 | + foreach ($data as $localRecordID) { |
|
185 | 185 | $collection[$localRecordID] = []; |
186 | 186 | } |
187 | - foreach($result as $depRecord) { |
|
187 | + foreach ($result as $depRecord) { |
|
188 | 188 | |
189 | 189 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
190 | 190 | $collection[$depRecord->$depKey][] = $depRecord->ID; |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | $localClass = $this->dataClass(); |
201 | 201 | $schema = DataObject::getSchema(); |
202 | 202 | |
203 | - foreach($withManyManys as $depSeq) { |
|
203 | + foreach ($withManyManys as $depSeq) { |
|
204 | 204 | $dep = $depSeq[0]; |
205 | 205 | $depClass = $manyManys[$dep]; |
206 | 206 | |
@@ -212,30 +212,30 @@ discard block |
||
212 | 212 | ]; |
213 | 213 | |
214 | 214 | $idsQuery = SQLSelect::create( |
215 | - implode(',',[$component['childField'],$component['parentField']]), |
|
215 | + implode(',', [$component['childField'], $component['parentField']]), |
|
216 | 216 | $component['join'], |
217 | 217 | [ |
218 | - $component['parentField'].' IN (' . implode(',',$data).')' |
|
218 | + $component['parentField'].' IN ('.implode(',', $data).')' |
|
219 | 219 | ] |
220 | 220 | )->execute(); |
221 | 221 | |
222 | 222 | $collection = []; |
223 | 223 | $relListReverted = []; |
224 | - foreach($idsQuery as $row){ |
|
224 | + foreach ($idsQuery as $row) { |
|
225 | 225 | $relID = $row[$component['childField']]; |
226 | 226 | $localID = $row[$component['parentField']]; |
227 | - if(!isset($collection[$localID])) $collection[$localID] = []; |
|
227 | + if (!isset($collection[$localID])) $collection[$localID] = []; |
|
228 | 228 | $collection[$localID][] = $relID; |
229 | - $relListReverted[$relID] = 1;//use ids as keys to avoid |
|
229 | + $relListReverted[$relID] = 1; //use ids as keys to avoid |
|
230 | 230 | } |
231 | 231 | |
232 | - $result = DataObject::get($depClass)->filter('ID',array_keys($relListReverted)); |
|
233 | - if(count($depSeq)>1){ |
|
232 | + $result = DataObject::get($depClass)->filter('ID', array_keys($relListReverted)); |
|
233 | + if (count($depSeq) > 1) { |
|
234 | 234 | $result = $result |
235 | - ->with(implode('.',array_slice($depSeq,1))); |
|
235 | + ->with(implode('.', array_slice($depSeq, 1))); |
|
236 | 236 | } |
237 | 237 | |
238 | - foreach($result as $depRecord) { |
|
238 | + foreach ($result as $depRecord) { |
|
239 | 239 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
240 | 240 | } |
241 | 241 | |
@@ -249,11 +249,11 @@ discard block |
||
249 | 249 | |
250 | 250 | public function fulfillEagerRelations(DataObject $item) |
251 | 251 | { |
252 | - foreach($this->_relatedMaps['has_one'] as $dep => $depInfo){ |
|
252 | + foreach ($this->_relatedMaps['has_one'] as $dep => $depInfo) { |
|
253 | 253 | $depClass = $depInfo['class']; |
254 | - if(isset($depInfo['map'][$item->ID])) { |
|
254 | + if (isset($depInfo['map'][$item->ID])) { |
|
255 | 255 | $depID = $depInfo['map'][$item->ID]; |
256 | - if(isset($this->_relatedCache[$depClass][$depID])) |
|
256 | + if (isset($this->_relatedCache[$depClass][$depID])) |
|
257 | 257 | { |
258 | 258 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
259 | 259 | $item->setComponent($dep, $depRecord); |
@@ -261,31 +261,31 @@ discard block |
||
261 | 261 | } |
262 | 262 | } |
263 | 263 | |
264 | - foreach($this->_relatedMaps['has_many'] as $dep => $depInfo){ |
|
264 | + foreach ($this->_relatedMaps['has_many'] as $dep => $depInfo) { |
|
265 | 265 | $depClass = $depInfo['class']; |
266 | 266 | $collection = []; |
267 | - if(isset($depInfo['map'][$item->ID])){ |
|
268 | - foreach($depInfo['map'][$item->ID] as $depID){ |
|
269 | - if(isset($this->_relatedCache[$depClass][$depID])) |
|
267 | + if (isset($depInfo['map'][$item->ID])) { |
|
268 | + foreach ($depInfo['map'][$item->ID] as $depID) { |
|
269 | + if (isset($this->_relatedCache[$depClass][$depID])) |
|
270 | 270 | { |
271 | 271 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
272 | 272 | $collection[] = $depRecord; |
273 | 273 | } |
274 | 274 | } |
275 | 275 | } |
276 | - if(!method_exists($item,'addEagerRelation')) { |
|
276 | + if (!method_exists($item, 'addEagerRelation')) { |
|
277 | 277 | throw new \Exception("Model {$item->ClassName} must include Gurucomkz\EagerLoading\EagerLoaderMultiAccessor trait to use eager loading for \$has_many"); |
278 | 278 | } |
279 | 279 | $item->addEagerRelation($dep, $collection); |
280 | 280 | } |
281 | 281 | |
282 | - foreach($this->_relatedMaps['many_many'] as $dep => $depInfo){ |
|
282 | + foreach ($this->_relatedMaps['many_many'] as $dep => $depInfo) { |
|
283 | 283 | $depClass = $depInfo['class']; |
284 | 284 | $collection = []; |
285 | - if(isset($depInfo['map'][$item->ID])){ |
|
286 | - foreach($depInfo['map'][$item->ID] as $depIDlist){ |
|
287 | - foreach($depIDlist as $depID){ |
|
288 | - if(isset($this->_relatedCache[$depClass][$depID])) |
|
285 | + if (isset($depInfo['map'][$item->ID])) { |
|
286 | + foreach ($depInfo['map'][$item->ID] as $depIDlist) { |
|
287 | + foreach ($depIDlist as $depID) { |
|
288 | + if (isset($this->_relatedCache[$depClass][$depID])) |
|
289 | 289 | { |
290 | 290 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
291 | 291 | $collection[] = $depRecord; |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | } |
294 | 294 | } |
295 | 295 | } |
296 | - if(!method_exists($item,'addEagerRelation')) { |
|
296 | + if (!method_exists($item, 'addEagerRelation')) { |
|
297 | 297 | throw new \Exception("Model {$item->ClassName} must include Gurucomkz\EagerLoading\EagerLoaderMultiAccessor trait to use eager loading for \$many_many"); |
298 | 298 | } |
299 | 299 | $item->addEagerRelation($dep, $collection); |
@@ -314,25 +314,25 @@ discard block |
||
314 | 314 | } |
315 | 315 | } |
316 | 316 | |
317 | - private function _prepareCache($all,$selected) |
|
317 | + private function _prepareCache($all, $selected) |
|
318 | 318 | { |
319 | - foreach($selected as $depSeq) { |
|
319 | + foreach ($selected as $depSeq) { |
|
320 | 320 | $dep = $depSeq[0]; |
321 | 321 | $depClass = $all[$dep]; |
322 | - if(!isset($this->_relatedCache[$depClass])) { $this->_relatedCache[$depClass] = []; } |
|
322 | + if (!isset($this->_relatedCache[$depClass])) { $this->_relatedCache[$depClass] = []; } |
|
323 | 323 | } |
324 | 324 | } |
325 | 325 | |
326 | 326 | |
327 | 327 | public static function EnsureArray($arr, $kfield = null) |
328 | 328 | { |
329 | - if(is_array($arr)) return $arr; |
|
329 | + if (is_array($arr)) return $arr; |
|
330 | 330 | $result = []; |
331 | - foreach($arr as $k => $v){ |
|
331 | + foreach ($arr as $k => $v) { |
|
332 | 332 | $key = $k; |
333 | - if($kfield!==null) { |
|
334 | - if(is_array($v) && isset($v[$kfield])) $key = $v[$kfield]; |
|
335 | - elseif(is_object($v) && isset($v->$kfield)) $key = $v->$kfield; |
|
333 | + if ($kfield !== null) { |
|
334 | + if (is_array($v) && isset($v[$kfield])) $key = $v[$kfield]; |
|
335 | + elseif (is_object($v) && isset($v->$kfield)) $key = $v->$kfield; |
|
336 | 336 | } |
337 | 337 | $result[$key] = $v; |
338 | 338 | } |
@@ -52,7 +52,9 @@ discard block |
||
52 | 52 | { |
53 | 53 | $result = []; |
54 | 54 | foreach($arr as $k => $v){ |
55 | - if(!isset($result[$v])) $result[$v] = []; |
|
55 | + if(!isset($result[$v])) { |
|
56 | + $result[$v] = []; |
|
57 | + } |
|
56 | 58 | $result[$v][] = $k; |
57 | 59 | } |
58 | 60 | return $result; |
@@ -76,7 +78,9 @@ discard block |
||
76 | 78 | private $relationsPrepared = false; |
77 | 79 | |
78 | 80 | public function prepareEagerRelations() { |
79 | - if($this->relationsPrepared) return; |
|
81 | + if($this->relationsPrepared) { |
|
82 | + return; |
|
83 | + } |
|
80 | 84 | $this->relationsPrepared = true; |
81 | 85 | $localClass = $this->dataClass(); |
82 | 86 | $config = Config::forClass($localClass); |
@@ -224,7 +228,9 @@ discard block |
||
224 | 228 | foreach($idsQuery as $row){ |
225 | 229 | $relID = $row[$component['childField']]; |
226 | 230 | $localID = $row[$component['parentField']]; |
227 | - if(!isset($collection[$localID])) $collection[$localID] = []; |
|
231 | + if(!isset($collection[$localID])) { |
|
232 | + $collection[$localID] = []; |
|
233 | + } |
|
228 | 234 | $collection[$localID][] = $relID; |
229 | 235 | $relListReverted[$relID] = 1;//use ids as keys to avoid |
230 | 236 | } |
@@ -326,13 +332,18 @@ discard block |
||
326 | 332 | |
327 | 333 | public static function EnsureArray($arr, $kfield = null) |
328 | 334 | { |
329 | - if(is_array($arr)) return $arr; |
|
335 | + if(is_array($arr)) { |
|
336 | + return $arr; |
|
337 | + } |
|
330 | 338 | $result = []; |
331 | 339 | foreach($arr as $k => $v){ |
332 | 340 | $key = $k; |
333 | 341 | if($kfield!==null) { |
334 | - if(is_array($v) && isset($v[$kfield])) $key = $v[$kfield]; |
|
335 | - elseif(is_object($v) && isset($v->$kfield)) $key = $v->$kfield; |
|
342 | + if(is_array($v) && isset($v[$kfield])) { |
|
343 | + $key = $v[$kfield]; |
|
344 | + } elseif(is_object($v) && isset($v->$kfield)) { |
|
345 | + $key = $v->$kfield; |
|
346 | + } |
|
336 | 347 | } |
337 | 348 | $result[$key] = $v; |
338 | 349 | } |