@@ -10,9 +10,9 @@ discard block |
||
10 | 10 | return $arr; |
11 | 11 | } |
12 | 12 | $result = []; |
13 | - foreach ($arr as $k => $v){ |
|
13 | + foreach ($arr as $k => $v) { |
|
14 | 14 | $key = $k; |
15 | - if ($kfield!==null) { |
|
15 | + if ($kfield !== null) { |
|
16 | 16 | if (is_array($v) && isset($v[$kfield])) $key = $v[$kfield]; |
17 | 17 | elseif (is_object($v) && isset($v->$kfield)) $key = $v->$kfield; |
18 | 18 | } |
@@ -21,10 +21,10 @@ discard block |
||
21 | 21 | return $result; |
22 | 22 | } |
23 | 23 | |
24 | - public static function extractField($arr,$field) |
|
24 | + public static function extractField($arr, $field) |
|
25 | 25 | { |
26 | 26 | $result = []; |
27 | - foreach ($arr as $record){ |
|
27 | + foreach ($arr as $record) { |
|
28 | 28 | $result[is_object($record) ? $record->ID : $record['ID']] = is_object($record) ? $record->$field : $record[$field]; |
29 | 29 | } |
30 | 30 | return $result; |
@@ -13,8 +13,11 @@ |
||
13 | 13 | foreach ($arr as $k => $v){ |
14 | 14 | $key = $k; |
15 | 15 | if ($kfield!==null) { |
16 | - if (is_array($v) && isset($v[$kfield])) $key = $v[$kfield]; |
|
17 | - elseif (is_object($v) && isset($v->$kfield)) $key = $v->$kfield; |
|
16 | + if (is_array($v) && isset($v[$kfield])) { |
|
17 | + $key = $v[$kfield]; |
|
18 | + } elseif (is_object($v) && isset($v->$kfield)) { |
|
19 | + $key = $v->$kfield; |
|
20 | + } |
|
18 | 21 | } |
19 | 22 | $result[$key] = $v; |
20 | 23 | } |
@@ -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()); |
@@ -58,35 +58,35 @@ discard block |
||
58 | 58 | private $relationsPrepared = false; |
59 | 59 | |
60 | 60 | public function prepareEagerRelations() { |
61 | - if($this->relationsPrepared) return; |
|
61 | + if ($this->relationsPrepared) return; |
|
62 | 62 | $this->relationsPrepared = true; |
63 | 63 | $localClass = $this->dataClass(); |
64 | 64 | $config = Config::forClass($localClass); |
65 | - $hasOnes = (array)$config->get('has_one'); |
|
66 | - $hasManys = (array)$config->get('has_many'); |
|
67 | - $manyManys = (array)$config->get('many_many'); |
|
65 | + $hasOnes = (array) $config->get('has_one'); |
|
66 | + $hasManys = (array) $config->get('has_many'); |
|
67 | + $manyManys = (array) $config->get('many_many'); |
|
68 | 68 | |
69 | 69 | //collect has_ones |
70 | - $withHasOnes = array_filter($this->withList,function($dep)use($hasOnes){ return array_key_exists($dep[0],$hasOnes); }); |
|
71 | - $withHasManys = array_filter($this->withList,function($dep)use($hasManys){ return array_key_exists($dep[0],$hasManys); }); |
|
72 | - $withManyManys = array_filter($this->withList,function($dep)use($manyManys){ return array_key_exists($dep[0],$manyManys); }); |
|
70 | + $withHasOnes = array_filter($this->withList, function($dep)use($hasOnes){ return array_key_exists($dep[0], $hasOnes); }); |
|
71 | + $withHasManys = array_filter($this->withList, function($dep)use($hasManys){ return array_key_exists($dep[0], $hasManys); }); |
|
72 | + $withManyManys = array_filter($this->withList, function($dep)use($manyManys){ return array_key_exists($dep[0], $manyManys); }); |
|
73 | 73 | |
74 | - if(!count($withHasOnes) && !count($withHasManys) && !count($withManyManys)){ |
|
74 | + if (!count($withHasOnes) && !count($withHasManys) && !count($withManyManys)) { |
|
75 | 75 | // do nothing if no matches |
76 | 76 | /** @todo report errors */ |
77 | 77 | return; |
78 | 78 | } |
79 | 79 | |
80 | 80 | $data = $this->column('ID'); |
81 | - if(count($withHasOnes)){ |
|
81 | + if (count($withHasOnes)) { |
|
82 | 82 | $this->_prepareCache($hasOnes, $withHasOnes); |
83 | 83 | $this->eagerLoadHasOne($data, $hasOnes, $withHasOnes); |
84 | 84 | } |
85 | - if(count($withHasManys)){ |
|
85 | + if (count($withHasManys)) { |
|
86 | 86 | $this->_prepareCache($hasManys, $withHasManys); |
87 | 87 | $this->eagerLoadHasMany($data, $hasManys, $withHasManys); |
88 | 88 | } |
89 | - if(count($withManyManys)){ |
|
89 | + if (count($withManyManys)) { |
|
90 | 90 | $this->_prepareCache($manyManys, $withManyManys); |
91 | 91 | $this->eagerLoadManyMany($data, $manyManys, $withManyManys); |
92 | 92 | } |
@@ -99,15 +99,15 @@ discard block |
||
99 | 99 | |
100 | 100 | //collect required IDS |
101 | 101 | $fields = ['ID']; |
102 | - foreach($withHasOnes as $depSeq) { |
|
102 | + foreach ($withHasOnes as $depSeq) { |
|
103 | 103 | $dep = $depSeq[0]; |
104 | 104 | $fields[] = "{$dep}ID"; |
105 | 105 | } |
106 | 106 | $table = Config::forClass($this->dataClass)->get('table_name'); |
107 | - $data = new SQLSelect(implode(',',$fields),[$table],["ID IN (".implode(',',$ids).")"]); |
|
108 | - $data = Utils::EnsureArray($data->execute(),'ID'); |
|
107 | + $data = new SQLSelect(implode(',', $fields), [$table], ["ID IN (".implode(',', $ids).")"]); |
|
108 | + $data = Utils::EnsureArray($data->execute(), 'ID'); |
|
109 | 109 | |
110 | - foreach($withHasOnes as $depSeq) { |
|
110 | + foreach ($withHasOnes as $depSeq) { |
|
111 | 111 | $dep = $depSeq[0]; |
112 | 112 | $depClass = $hasOnes[$dep]; |
113 | 113 | |
@@ -119,17 +119,17 @@ discard block |
||
119 | 119 | |
120 | 120 | $component = $schema->hasOneComponent($this->dataClass, $dep); |
121 | 121 | |
122 | - $descriptor['map'] = Utils::extractField($data,$descriptor['localField']); |
|
122 | + $descriptor['map'] = Utils::extractField($data, $descriptor['localField']); |
|
123 | 123 | $uniqueIDs = array_unique($descriptor['map']); |
124 | - while(count($uniqueIDs)) { |
|
125 | - $IDsubset = array_splice($uniqueIDs,0,self::ID_LIMIT); |
|
126 | - $result = DataObject::get($depClass)->filter('ID',$IDsubset); |
|
127 | - if(count($depSeq)>1){ |
|
124 | + while (count($uniqueIDs)) { |
|
125 | + $IDsubset = array_splice($uniqueIDs, 0, self::ID_LIMIT); |
|
126 | + $result = DataObject::get($depClass)->filter('ID', $IDsubset); |
|
127 | + if (count($depSeq) > 1) { |
|
128 | 128 | $result = $result |
129 | - ->with(implode('.',array_slice($depSeq,1))); |
|
129 | + ->with(implode('.', array_slice($depSeq, 1))); |
|
130 | 130 | } |
131 | 131 | |
132 | - foreach($result as $depRecord) { |
|
132 | + foreach ($result as $depRecord) { |
|
133 | 133 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
134 | 134 | } |
135 | 135 | } |
@@ -142,9 +142,9 @@ discard block |
||
142 | 142 | public function eagerLoadHasMany($data, $hasManys, $withHasManys) |
143 | 143 | { |
144 | 144 | $localClass = $this->dataClass(); |
145 | - $localClassTail = basename(str_replace('\\','/',$localClass)); |
|
145 | + $localClassTail = basename(str_replace('\\', '/', $localClass)); |
|
146 | 146 | |
147 | - foreach($withHasManys as $depSeq) { |
|
147 | + foreach ($withHasManys as $depSeq) { |
|
148 | 148 | $dep = $depSeq[0]; |
149 | 149 | $depClass = $hasManys[$dep]; |
150 | 150 | $localNameInDep = $localClassTail; |
@@ -155,18 +155,18 @@ discard block |
||
155 | 155 | 'remoteField' => $depKey, |
156 | 156 | 'map' => [], |
157 | 157 | ]; |
158 | - $result = DataObject::get($depClass)->filter($depKey,$data); |
|
159 | - if(count($depSeq)>1){ |
|
158 | + $result = DataObject::get($depClass)->filter($depKey, $data); |
|
159 | + if (count($depSeq) > 1) { |
|
160 | 160 | $result = $result |
161 | - ->with(implode('.',array_slice($depSeq,1))); |
|
161 | + ->with(implode('.', array_slice($depSeq, 1))); |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | $collection = []; |
165 | 165 | |
166 | - foreach($data as $localRecordID){ |
|
166 | + foreach ($data as $localRecordID) { |
|
167 | 167 | $collection[$localRecordID] = []; |
168 | 168 | } |
169 | - foreach($result as $depRecord) { |
|
169 | + foreach ($result as $depRecord) { |
|
170 | 170 | |
171 | 171 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
172 | 172 | $collection[$depRecord->$depKey][] = $depRecord->ID; |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | $localClass = $this->dataClass(); |
183 | 183 | $schema = DataObject::getSchema(); |
184 | 184 | |
185 | - foreach($withManyManys as $depSeq) { |
|
185 | + foreach ($withManyManys as $depSeq) { |
|
186 | 186 | $dep = $depSeq[0]; |
187 | 187 | $depClass = $manyManys[$dep]; |
188 | 188 | |
@@ -194,30 +194,30 @@ discard block |
||
194 | 194 | ]; |
195 | 195 | |
196 | 196 | $idsQuery = SQLSelect::create( |
197 | - implode(',',[$component['childField'],$component['parentField']]), |
|
197 | + implode(',', [$component['childField'], $component['parentField']]), |
|
198 | 198 | $component['join'], |
199 | 199 | [ |
200 | - $component['parentField'].' IN (' . implode(',',$data).')' |
|
200 | + $component['parentField'].' IN ('.implode(',', $data).')' |
|
201 | 201 | ] |
202 | 202 | )->execute(); |
203 | 203 | |
204 | 204 | $collection = []; |
205 | 205 | $relListReverted = []; |
206 | - foreach($idsQuery as $row){ |
|
206 | + foreach ($idsQuery as $row) { |
|
207 | 207 | $relID = $row[$component['childField']]; |
208 | 208 | $localID = $row[$component['parentField']]; |
209 | - if(!isset($collection[$localID])) $collection[$localID] = []; |
|
209 | + if (!isset($collection[$localID])) $collection[$localID] = []; |
|
210 | 210 | $collection[$localID][] = $relID; |
211 | - $relListReverted[$relID] = 1;//use ids as keys to avoid |
|
211 | + $relListReverted[$relID] = 1; //use ids as keys to avoid |
|
212 | 212 | } |
213 | 213 | |
214 | - $result = DataObject::get($depClass)->filter('ID',array_keys($relListReverted)); |
|
215 | - if(count($depSeq)>1){ |
|
214 | + $result = DataObject::get($depClass)->filter('ID', array_keys($relListReverted)); |
|
215 | + if (count($depSeq) > 1) { |
|
216 | 216 | $result = $result |
217 | - ->with(implode('.',array_slice($depSeq,1))); |
|
217 | + ->with(implode('.', array_slice($depSeq, 1))); |
|
218 | 218 | } |
219 | 219 | |
220 | - foreach($result as $depRecord) { |
|
220 | + foreach ($result as $depRecord) { |
|
221 | 221 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
222 | 222 | } |
223 | 223 | |
@@ -231,11 +231,11 @@ discard block |
||
231 | 231 | |
232 | 232 | public function fulfillEagerRelations(DataObject $item) |
233 | 233 | { |
234 | - foreach($this->_relatedMaps['has_one'] as $dep => $depInfo){ |
|
234 | + foreach ($this->_relatedMaps['has_one'] as $dep => $depInfo) { |
|
235 | 235 | $depClass = $depInfo['class']; |
236 | - if(isset($depInfo['map'][$item->ID])) { |
|
236 | + if (isset($depInfo['map'][$item->ID])) { |
|
237 | 237 | $depID = $depInfo['map'][$item->ID]; |
238 | - if(isset($this->_relatedCache[$depClass][$depID])) |
|
238 | + if (isset($this->_relatedCache[$depClass][$depID])) |
|
239 | 239 | { |
240 | 240 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
241 | 241 | $item->setComponent($dep, $depRecord); |
@@ -243,31 +243,31 @@ discard block |
||
243 | 243 | } |
244 | 244 | } |
245 | 245 | |
246 | - foreach($this->_relatedMaps['has_many'] as $dep => $depInfo){ |
|
246 | + foreach ($this->_relatedMaps['has_many'] as $dep => $depInfo) { |
|
247 | 247 | $depClass = $depInfo['class']; |
248 | 248 | $collection = []; |
249 | - if(isset($depInfo['map'][$item->ID])){ |
|
250 | - foreach($depInfo['map'][$item->ID] as $depID){ |
|
251 | - if(isset($this->_relatedCache[$depClass][$depID])) |
|
249 | + if (isset($depInfo['map'][$item->ID])) { |
|
250 | + foreach ($depInfo['map'][$item->ID] as $depID) { |
|
251 | + if (isset($this->_relatedCache[$depClass][$depID])) |
|
252 | 252 | { |
253 | 253 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
254 | 254 | $collection[] = $depRecord; |
255 | 255 | } |
256 | 256 | } |
257 | 257 | } |
258 | - if(!method_exists($item,'addEagerRelation')) { |
|
258 | + if (!method_exists($item, 'addEagerRelation')) { |
|
259 | 259 | throw new \Exception("Model {$item->ClassName} must include Gurucomkz\EagerLoading\EagerLoaderMultiAccessor trait to use eager loading for \$has_many"); |
260 | 260 | } |
261 | 261 | $item->addEagerRelation($dep, $collection); |
262 | 262 | } |
263 | 263 | |
264 | - foreach($this->_relatedMaps['many_many'] as $dep => $depInfo){ |
|
264 | + foreach ($this->_relatedMaps['many_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 $depIDlist){ |
|
269 | - foreach($depIDlist as $depID){ |
|
270 | - if(isset($this->_relatedCache[$depClass][$depID])) |
|
267 | + if (isset($depInfo['map'][$item->ID])) { |
|
268 | + foreach ($depInfo['map'][$item->ID] as $depIDlist) { |
|
269 | + foreach ($depIDlist as $depID) { |
|
270 | + if (isset($this->_relatedCache[$depClass][$depID])) |
|
271 | 271 | { |
272 | 272 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
273 | 273 | $collection[] = $depRecord; |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | } |
276 | 276 | } |
277 | 277 | } |
278 | - if(!method_exists($item,'addEagerRelation')) { |
|
278 | + if (!method_exists($item, 'addEagerRelation')) { |
|
279 | 279 | throw new \Exception("Model {$item->ClassName} must include Gurucomkz\EagerLoading\EagerLoaderMultiAccessor trait to use eager loading for \$many_many"); |
280 | 280 | } |
281 | 281 | $item->addEagerRelation($dep, $collection); |
@@ -296,12 +296,12 @@ discard block |
||
296 | 296 | } |
297 | 297 | } |
298 | 298 | |
299 | - private function _prepareCache($all,$selected) |
|
299 | + private function _prepareCache($all, $selected) |
|
300 | 300 | { |
301 | - foreach($selected as $depSeq) { |
|
301 | + foreach ($selected as $depSeq) { |
|
302 | 302 | $dep = $depSeq[0]; |
303 | 303 | $depClass = $all[$dep]; |
304 | - if(!isset($this->_relatedCache[$depClass])) { $this->_relatedCache[$depClass] = []; } |
|
304 | + if (!isset($this->_relatedCache[$depClass])) { $this->_relatedCache[$depClass] = []; } |
|
305 | 305 | } |
306 | 306 | } |
307 | 307 |
@@ -58,7 +58,9 @@ discard block |
||
58 | 58 | private $relationsPrepared = false; |
59 | 59 | |
60 | 60 | public function prepareEagerRelations() { |
61 | - if($this->relationsPrepared) return; |
|
61 | + if($this->relationsPrepared) { |
|
62 | + return; |
|
63 | + } |
|
62 | 64 | $this->relationsPrepared = true; |
63 | 65 | $localClass = $this->dataClass(); |
64 | 66 | $config = Config::forClass($localClass); |
@@ -206,7 +208,9 @@ discard block |
||
206 | 208 | foreach($idsQuery as $row){ |
207 | 209 | $relID = $row[$component['childField']]; |
208 | 210 | $localID = $row[$component['parentField']]; |
209 | - if(!isset($collection[$localID])) $collection[$localID] = []; |
|
211 | + if(!isset($collection[$localID])) { |
|
212 | + $collection[$localID] = []; |
|
213 | + } |
|
210 | 214 | $collection[$localID][] = $relID; |
211 | 215 | $relListReverted[$relID] = 1;//use ids as keys to avoid |
212 | 216 | } |