@@ -13,12 +13,12 @@ |
||
13 | 13 | public $withList = []; |
14 | 14 | public function with($list) { |
15 | 15 | // return $this->owner; |
16 | - if(!isset($this->owner->withList)){ |
|
16 | + if (!isset($this->owner->withList)) { |
|
17 | 17 | $this->owner->withList = []; |
18 | 18 | } |
19 | - if(!is_array($list)) $list = [$list]; |
|
20 | - $list = array_map(function($e){return explode('.',$e); },$list); |
|
21 | - $this->owner->withList = array_merge($this->owner->withList,$list); |
|
19 | + if (!is_array($list)) $list = [$list]; |
|
20 | + $list = array_map(function($e) {return explode('.', $e); },$list); |
|
21 | + $this->owner->withList = array_merge($this->owner->withList, $list); |
|
22 | 22 | return EagerLoadedDataList::cloneFrom($this->owner); |
23 | 23 | } |
24 | 24 |
@@ -16,7 +16,9 @@ |
||
16 | 16 | if(!isset($this->owner->withList)){ |
17 | 17 | $this->owner->withList = []; |
18 | 18 | } |
19 | - if(!is_array($list)) $list = [$list]; |
|
19 | + if(!is_array($list)) { |
|
20 | + $list = [$list]; |
|
21 | + } |
|
20 | 22 | $list = array_map(function($e){return explode('.',$e); },$list); |
21 | 23 | $this->owner->withList = array_merge($this->owner->withList,$list); |
22 | 24 | return EagerLoadedDataList::cloneFrom($this->owner); |
@@ -34,21 +34,21 @@ |
||
34 | 34 | $config = Config::forClass($class); |
35 | 35 | $vars = $gridField->getForm()->getController()->getRequest()->requestVars(); |
36 | 36 | $eager = $config->get('eager_loading'); |
37 | - if(in_array('Export to CSV',$vars)) { |
|
37 | + if (in_array('Export to CSV', $vars)) { |
|
38 | 38 | $export_fields = $config->get('export_fields'); |
39 | - if($export_fields) { |
|
39 | + if ($export_fields) { |
|
40 | 40 | $export_eager = []; |
41 | - foreach($export_fields as $field=>$_title) { |
|
42 | - $parts = explode('.',$field); |
|
43 | - if(count($parts) < 2) continue; |
|
44 | - $main = implode('.', array_slice($parts,0,-1)); |
|
45 | - if(!in_array($main,$eager)){ |
|
41 | + foreach ($export_fields as $field=>$_title) { |
|
42 | + $parts = explode('.', $field); |
|
43 | + if (count($parts) < 2) continue; |
|
44 | + $main = implode('.', array_slice($parts, 0, -1)); |
|
45 | + if (!in_array($main, $eager)) { |
|
46 | 46 | $eager[] = $main; |
47 | 47 | } |
48 | 48 | } |
49 | 49 | } |
50 | 50 | } |
51 | - if($eager) return $dataList->with($eager); |
|
51 | + if ($eager) return $dataList->with($eager); |
|
52 | 52 | return $dataList; |
53 | 53 | } |
54 | 54 | } |
@@ -40,7 +40,9 @@ discard block |
||
40 | 40 | $export_eager = []; |
41 | 41 | foreach($export_fields as $field=>$_title) { |
42 | 42 | $parts = explode('.',$field); |
43 | - if(count($parts) < 2) continue; |
|
43 | + if(count($parts) < 2) { |
|
44 | + continue; |
|
45 | + } |
|
44 | 46 | $main = implode('.', array_slice($parts,0,-1)); |
45 | 47 | if(!in_array($main,$eager)){ |
46 | 48 | $eager[] = $main; |
@@ -48,7 +50,9 @@ discard block |
||
48 | 50 | } |
49 | 51 | } |
50 | 52 | } |
51 | - if($eager) return $dataList->with($eager); |
|
53 | + if($eager) { |
|
54 | + return $dataList->with($eager); |
|
55 | + } |
|
52 | 56 | return $dataList; |
53 | 57 | } |
54 | 58 | } |
@@ -22,7 +22,7 @@ |
||
22 | 22 | */ |
23 | 23 | public function tryEagerLoadingRelation($method) |
24 | 24 | { |
25 | - if(!isset($this->_eagerLoadingCache[$method])) { |
|
25 | + if (!isset($this->_eagerLoadingCache[$method])) { |
|
26 | 26 | return null; |
27 | 27 | } |
28 | 28 | return $this->_eagerLoadingCache[$method]; |
@@ -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; |
@@ -74,35 +74,35 @@ discard block |
||
74 | 74 | } |
75 | 75 | |
76 | 76 | public function prepareEagerRelations() { |
77 | - if($this->relationsPrepared) return; |
|
77 | + if ($this->relationsPrepared) return; |
|
78 | 78 | $this->relationsPrepared = true; |
79 | 79 | $localClass = $this->dataClass(); |
80 | 80 | $config = Config::forClass($localClass); |
81 | - $hasOnes = (array)$config->get('has_one'); |
|
82 | - $hasManys = (array)$config->get('has_many'); |
|
83 | - $manyManys = (array)$config->get('many_many'); |
|
81 | + $hasOnes = (array) $config->get('has_one'); |
|
82 | + $hasManys = (array) $config->get('has_many'); |
|
83 | + $manyManys = (array) $config->get('many_many'); |
|
84 | 84 | |
85 | 85 | //collect has_ones |
86 | - $withHasOnes = array_filter($this->withList,function($dep)use($hasOnes){ return array_key_exists($dep[0],$hasOnes); }); |
|
87 | - $withHasManys = array_filter($this->withList,function($dep)use($hasManys){ return array_key_exists($dep[0],$hasManys); }); |
|
88 | - $withManyManys = array_filter($this->withList,function($dep)use($manyManys){ return array_key_exists($dep[0],$manyManys); }); |
|
86 | + $withHasOnes = array_filter($this->withList, function($dep)use($hasOnes){ return array_key_exists($dep[0], $hasOnes); }); |
|
87 | + $withHasManys = array_filter($this->withList, function($dep)use($hasManys){ return array_key_exists($dep[0], $hasManys); }); |
|
88 | + $withManyManys = array_filter($this->withList, function($dep)use($manyManys){ return array_key_exists($dep[0], $manyManys); }); |
|
89 | 89 | |
90 | - if(!count($withHasOnes) && !count($withHasManys) && !count($withManyManys)){ |
|
90 | + if (!count($withHasOnes) && !count($withHasManys) && !count($withManyManys)) { |
|
91 | 91 | // do nothing if no matches |
92 | 92 | /** @todo report errors */ |
93 | 93 | return; |
94 | 94 | } |
95 | 95 | |
96 | 96 | $data = $this->column('ID'); |
97 | - if(count($withHasOnes)){ |
|
97 | + if (count($withHasOnes)) { |
|
98 | 98 | $this->_prepareCache($hasOnes, $withHasOnes); |
99 | 99 | $this->eagerLoadHasOne($data, $hasOnes, $withHasOnes); |
100 | 100 | } |
101 | - if(count($withHasManys)){ |
|
101 | + if (count($withHasManys)) { |
|
102 | 102 | $this->_prepareCache($hasManys, $withHasManys); |
103 | 103 | $this->eagerLoadHasMany($data, $hasManys, $withHasManys); |
104 | 104 | } |
105 | - if(count($withManyManys)){ |
|
105 | + if (count($withManyManys)) { |
|
106 | 106 | $this->_prepareCache($manyManys, $withManyManys); |
107 | 107 | $this->eagerLoadManyMany($data, $manyManys, $withManyManys); |
108 | 108 | } |
@@ -115,15 +115,15 @@ discard block |
||
115 | 115 | |
116 | 116 | //collect required IDS |
117 | 117 | $fields = ['ID']; |
118 | - foreach($withHasOnes as $depSeq) { |
|
118 | + foreach ($withHasOnes as $depSeq) { |
|
119 | 119 | $dep = $depSeq[0]; |
120 | 120 | $fields[] = "{$dep}ID"; |
121 | 121 | } |
122 | 122 | $table = Config::forClass($this->dataClass)->get('table_name'); |
123 | - $data = new SQLSelect(implode(',',$fields),[$table],["ID IN (".implode(',',$ids).")"]); |
|
124 | - $data = self::EnsureArray($data->execute(),'ID'); |
|
123 | + $data = new SQLSelect(implode(',', $fields), [$table], ["ID IN (".implode(',', $ids).")"]); |
|
124 | + $data = self::EnsureArray($data->execute(), 'ID'); |
|
125 | 125 | |
126 | - foreach($withHasOnes as $depSeq) { |
|
126 | + foreach ($withHasOnes as $depSeq) { |
|
127 | 127 | $dep = $depSeq[0]; |
128 | 128 | $depClass = $hasOnes[$dep]; |
129 | 129 | |
@@ -135,17 +135,17 @@ discard block |
||
135 | 135 | |
136 | 136 | $component = $schema->hasOneComponent($this->dataClass, $dep); |
137 | 137 | |
138 | - $descriptor['map'] = self::extractField($data,$descriptor['localField']); |
|
138 | + $descriptor['map'] = self::extractField($data, $descriptor['localField']); |
|
139 | 139 | $uniqueIDs = array_unique($descriptor['map']); |
140 | - while(count($uniqueIDs)) { |
|
141 | - $IDsubset = array_splice($uniqueIDs,0,self::ID_LIMIT); |
|
142 | - $result = DataObject::get($depClass)->filter('ID',$IDsubset); |
|
143 | - if(count($depSeq)>1){ |
|
140 | + while (count($uniqueIDs)) { |
|
141 | + $IDsubset = array_splice($uniqueIDs, 0, self::ID_LIMIT); |
|
142 | + $result = DataObject::get($depClass)->filter('ID', $IDsubset); |
|
143 | + if (count($depSeq) > 1) { |
|
144 | 144 | $result = $result |
145 | - ->with(implode('.',array_slice($depSeq,1))); |
|
145 | + ->with(implode('.', array_slice($depSeq, 1))); |
|
146 | 146 | } |
147 | 147 | |
148 | - foreach($result as $depRecord) { |
|
148 | + foreach ($result as $depRecord) { |
|
149 | 149 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
150 | 150 | } |
151 | 151 | } |
@@ -158,9 +158,9 @@ discard block |
||
158 | 158 | public function eagerLoadHasMany($data, $hasManys, $withHasManys) |
159 | 159 | { |
160 | 160 | $localClass = $this->dataClass(); |
161 | - $localClassTail = basename(str_replace('\\','/',$localClass)); |
|
161 | + $localClassTail = basename(str_replace('\\', '/', $localClass)); |
|
162 | 162 | |
163 | - foreach($withHasManys as $depSeq) { |
|
163 | + foreach ($withHasManys as $depSeq) { |
|
164 | 164 | $dep = $depSeq[0]; |
165 | 165 | $depClass = $hasManys[$dep]; |
166 | 166 | $localNameInDep = $localClassTail; |
@@ -171,18 +171,18 @@ discard block |
||
171 | 171 | 'remoteField' => $depKey, |
172 | 172 | 'map' => [], |
173 | 173 | ]; |
174 | - $result = DataObject::get($depClass)->filter($depKey,$data); |
|
175 | - if(count($depSeq)>1){ |
|
174 | + $result = DataObject::get($depClass)->filter($depKey, $data); |
|
175 | + if (count($depSeq) > 1) { |
|
176 | 176 | $result = $result |
177 | - ->with(implode('.',array_slice($depSeq,1))); |
|
177 | + ->with(implode('.', array_slice($depSeq, 1))); |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | $collection = []; |
181 | 181 | |
182 | - foreach($data as $localRecordID){ |
|
182 | + foreach ($data as $localRecordID) { |
|
183 | 183 | $collection[$localRecordID] = []; |
184 | 184 | } |
185 | - foreach($result as $depRecord) { |
|
185 | + foreach ($result as $depRecord) { |
|
186 | 186 | |
187 | 187 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
188 | 188 | $collection[$depRecord->$depKey][] = $depRecord->ID; |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | $localClass = $this->dataClass(); |
199 | 199 | $schema = DataObject::getSchema(); |
200 | 200 | |
201 | - foreach($withManyManys as $depSeq) { |
|
201 | + foreach ($withManyManys as $depSeq) { |
|
202 | 202 | $dep = $depSeq[0]; |
203 | 203 | $depClass = $manyManys[$dep]; |
204 | 204 | |
@@ -210,30 +210,30 @@ discard block |
||
210 | 210 | ]; |
211 | 211 | |
212 | 212 | $idsQuery = SQLSelect::create( |
213 | - implode(',',[$component['childField'],$component['parentField']]), |
|
213 | + implode(',', [$component['childField'], $component['parentField']]), |
|
214 | 214 | $component['join'], |
215 | 215 | [ |
216 | - $component['parentField'].' IN (' . implode(',',$data).')' |
|
216 | + $component['parentField'].' IN ('.implode(',', $data).')' |
|
217 | 217 | ] |
218 | 218 | )->execute(); |
219 | 219 | |
220 | 220 | $collection = []; |
221 | 221 | $relListReverted = []; |
222 | - foreach($idsQuery as $row){ |
|
222 | + foreach ($idsQuery as $row) { |
|
223 | 223 | $relID = $row[$component['childField']]; |
224 | 224 | $localID = $row[$component['parentField']]; |
225 | - if(!isset($collection[$localID])) $collection[$localID] = []; |
|
225 | + if (!isset($collection[$localID])) $collection[$localID] = []; |
|
226 | 226 | $collection[$localID][] = $relID; |
227 | - $relListReverted[$relID] = 1;//use ids as keys to avoid |
|
227 | + $relListReverted[$relID] = 1; //use ids as keys to avoid |
|
228 | 228 | } |
229 | 229 | |
230 | - $result = DataObject::get($depClass)->filter('ID',array_keys($relListReverted)); |
|
231 | - if(count($depSeq)>1){ |
|
230 | + $result = DataObject::get($depClass)->filter('ID', array_keys($relListReverted)); |
|
231 | + if (count($depSeq) > 1) { |
|
232 | 232 | $result = $result |
233 | - ->with(implode('.',array_slice($depSeq,1))); |
|
233 | + ->with(implode('.', array_slice($depSeq, 1))); |
|
234 | 234 | } |
235 | 235 | |
236 | - foreach($result as $depRecord) { |
|
236 | + foreach ($result as $depRecord) { |
|
237 | 237 | $this->_relatedCache[$depClass][$depRecord->ID] = $depRecord; |
238 | 238 | } |
239 | 239 | |
@@ -247,11 +247,11 @@ discard block |
||
247 | 247 | |
248 | 248 | public function fulfillEagerRelations(DataObject $item) |
249 | 249 | { |
250 | - foreach($this->_relatedMaps['has_one'] as $dep => $depInfo){ |
|
250 | + foreach ($this->_relatedMaps['has_one'] as $dep => $depInfo) { |
|
251 | 251 | $depClass = $depInfo['class']; |
252 | - if(isset($depInfo['map'][$item->ID])) { |
|
252 | + if (isset($depInfo['map'][$item->ID])) { |
|
253 | 253 | $depID = $depInfo['map'][$item->ID]; |
254 | - if(isset($this->_relatedCache[$depClass][$depID])) |
|
254 | + if (isset($this->_relatedCache[$depClass][$depID])) |
|
255 | 255 | { |
256 | 256 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
257 | 257 | $item->setComponent($dep, $depRecord); |
@@ -259,12 +259,12 @@ discard block |
||
259 | 259 | } |
260 | 260 | } |
261 | 261 | |
262 | - foreach($this->_relatedMaps['has_many'] as $dep => $depInfo){ |
|
262 | + foreach ($this->_relatedMaps['has_many'] as $dep => $depInfo) { |
|
263 | 263 | $depClass = $depInfo['class']; |
264 | 264 | $collection = []; |
265 | - if(isset($depInfo['map'][$item->ID])){ |
|
266 | - foreach($depInfo['map'][$item->ID] as $depID){ |
|
267 | - if(isset($this->_relatedCache[$depClass][$depID])) |
|
265 | + if (isset($depInfo['map'][$item->ID])) { |
|
266 | + foreach ($depInfo['map'][$item->ID] as $depID) { |
|
267 | + if (isset($this->_relatedCache[$depClass][$depID])) |
|
268 | 268 | { |
269 | 269 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
270 | 270 | $collection[] = $depRecord; |
@@ -274,13 +274,13 @@ discard block |
||
274 | 274 | $item->addEagerRelation($dep, $collection); |
275 | 275 | } |
276 | 276 | |
277 | - foreach($this->_relatedMaps['many_many'] as $dep => $depInfo){ |
|
277 | + foreach ($this->_relatedMaps['many_many'] as $dep => $depInfo) { |
|
278 | 278 | $depClass = $depInfo['class']; |
279 | 279 | $collection = []; |
280 | - if(isset($depInfo['map'][$item->ID])){ |
|
281 | - foreach($depInfo['map'][$item->ID] as $depIDlist){ |
|
282 | - foreach($depIDlist as $depID){ |
|
283 | - if(isset($this->_relatedCache[$depClass][$depID])) |
|
280 | + if (isset($depInfo['map'][$item->ID])) { |
|
281 | + foreach ($depInfo['map'][$item->ID] as $depIDlist) { |
|
282 | + foreach ($depIDlist as $depID) { |
|
283 | + if (isset($this->_relatedCache[$depClass][$depID])) |
|
284 | 284 | { |
285 | 285 | $depRecord = $this->_relatedCache[$depClass][$depID]; |
286 | 286 | $collection[] = $depRecord; |
@@ -306,25 +306,25 @@ discard block |
||
306 | 306 | } |
307 | 307 | } |
308 | 308 | |
309 | - private function _prepareCache($all,$selected) |
|
309 | + private function _prepareCache($all, $selected) |
|
310 | 310 | { |
311 | - foreach($selected as $depSeq) { |
|
311 | + foreach ($selected as $depSeq) { |
|
312 | 312 | $dep = $depSeq[0]; |
313 | 313 | $depClass = $all[$dep]; |
314 | - if(!isset($this->_relatedCache[$depClass])) { $this->_relatedCache[$depClass] = []; } |
|
314 | + if (!isset($this->_relatedCache[$depClass])) { $this->_relatedCache[$depClass] = []; } |
|
315 | 315 | } |
316 | 316 | } |
317 | 317 | |
318 | 318 | |
319 | 319 | public static function EnsureArray($arr, $kfield = null) |
320 | 320 | { |
321 | - if(is_array($arr)) return $arr; |
|
321 | + if (is_array($arr)) return $arr; |
|
322 | 322 | $result = []; |
323 | - foreach($arr as $k => $v){ |
|
323 | + foreach ($arr as $k => $v) { |
|
324 | 324 | $key = $k; |
325 | - if($kfield!==null) { |
|
326 | - if(is_array($v) && isset($v[$kfield])) $key = $v[$kfield]; |
|
327 | - elseif(is_object($v) && isset($v->$kfield)) $key = $v->$kfield; |
|
325 | + if ($kfield !== null) { |
|
326 | + if (is_array($v) && isset($v[$kfield])) $key = $v[$kfield]; |
|
327 | + elseif (is_object($v) && isset($v->$kfield)) $key = $v->$kfield; |
|
328 | 328 | } |
329 | 329 | $result[$key] = $v; |
330 | 330 | } |
@@ -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; |
@@ -74,7 +76,9 @@ discard block |
||
74 | 76 | } |
75 | 77 | |
76 | 78 | public function prepareEagerRelations() { |
77 | - if($this->relationsPrepared) return; |
|
79 | + if($this->relationsPrepared) { |
|
80 | + return; |
|
81 | + } |
|
78 | 82 | $this->relationsPrepared = true; |
79 | 83 | $localClass = $this->dataClass(); |
80 | 84 | $config = Config::forClass($localClass); |
@@ -222,7 +226,9 @@ discard block |
||
222 | 226 | foreach($idsQuery as $row){ |
223 | 227 | $relID = $row[$component['childField']]; |
224 | 228 | $localID = $row[$component['parentField']]; |
225 | - if(!isset($collection[$localID])) $collection[$localID] = []; |
|
229 | + if(!isset($collection[$localID])) { |
|
230 | + $collection[$localID] = []; |
|
231 | + } |
|
226 | 232 | $collection[$localID][] = $relID; |
227 | 233 | $relListReverted[$relID] = 1;//use ids as keys to avoid |
228 | 234 | } |
@@ -318,13 +324,18 @@ discard block |
||
318 | 324 | |
319 | 325 | public static function EnsureArray($arr, $kfield = null) |
320 | 326 | { |
321 | - if(is_array($arr)) return $arr; |
|
327 | + if(is_array($arr)) { |
|
328 | + return $arr; |
|
329 | + } |
|
322 | 330 | $result = []; |
323 | 331 | foreach($arr as $k => $v){ |
324 | 332 | $key = $k; |
325 | 333 | if($kfield!==null) { |
326 | - if(is_array($v) && isset($v[$kfield])) $key = $v[$kfield]; |
|
327 | - elseif(is_object($v) && isset($v->$kfield)) $key = $v->$kfield; |
|
334 | + if(is_array($v) && isset($v[$kfield])) { |
|
335 | + $key = $v[$kfield]; |
|
336 | + } elseif(is_object($v) && isset($v->$kfield)) { |
|
337 | + $key = $v->$kfield; |
|
338 | + } |
|
328 | 339 | } |
329 | 340 | $result[$key] = $v; |
330 | 341 | } |
@@ -8,8 +8,8 @@ |
||
8 | 8 | trait EagerLoaderMultiAccessor { |
9 | 9 | public function __call($method, $arguments) |
10 | 10 | { |
11 | - if($method !== 'tryEagerLoadingRelation'){ |
|
12 | - if(null !== ($eagerResult = $this->tryEagerLoadingRelation($method))) { |
|
11 | + if ($method !== 'tryEagerLoadingRelation') { |
|
12 | + if (null !== ($eagerResult = $this->tryEagerLoadingRelation($method))) { |
|
13 | 13 | return $eagerResult; |
14 | 14 | } |
15 | 15 | } |