@@ -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 | } |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | trait EagerLoaderMultiAccessor { |
| 12 | 12 | public function __call($method, $arguments) |
| 13 | 13 | { |
| 14 | - if ($method !== 'tryEagerLoadingRelation'){ |
|
| 14 | + if ($method !== 'tryEagerLoadingRelation') { |
|
| 15 | 15 | if (null !== ($eagerResult = $this->tryEagerLoadingRelation($method))) { |
| 16 | 16 | return $eagerResult; |
| 17 | 17 | } |
@@ -23,7 +23,7 @@ |
||
| 23 | 23 | public function updateProxy(ProxyGenerator &$proxy) |
| 24 | 24 | { |
| 25 | 25 | // In the closure, $this is the proxied database |
| 26 | - $callback = function ($args, $next) { |
|
| 26 | + $callback = function($args, $next) { |
|
| 27 | 27 | |
| 28 | 28 | // The first argument is always the sql query |
| 29 | 29 | $sql = $args[0]; |
@@ -29,12 +29,12 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | $players = Player::get()->with('Team'); |
| 31 | 31 | |
| 32 | - foreach($players as $player) { |
|
| 32 | + foreach ($players as $player) { |
|
| 33 | 33 | $team = $player->Team->Title; |
| 34 | 34 | } |
| 35 | 35 | $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); |
| 36 | 36 | |
| 37 | - $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count); |
|
| 37 | + $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | public function testHasMany() |
@@ -45,12 +45,12 @@ discard block |
||
| 45 | 45 | |
| 46 | 46 | $teams = Team::get()->with('Players'); |
| 47 | 47 | |
| 48 | - foreach($teams as $team) { |
|
| 48 | + foreach ($teams as $team) { |
|
| 49 | 49 | $players = $team->Players()->map()->toArray(); |
| 50 | 50 | } |
| 51 | 51 | $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); |
| 52 | 52 | |
| 53 | - $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count); |
|
| 53 | + $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | public function testManyMany() |
@@ -61,13 +61,13 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | $players = Player::get()->with('Listens'); |
| 63 | 63 | |
| 64 | - foreach($players as $player) { |
|
| 64 | + foreach ($players as $player) { |
|
| 65 | 65 | $music = $player->Listens()->map()->toArray(); |
| 66 | 66 | // print_r($music); |
| 67 | 67 | } |
| 68 | 68 | $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); |
| 69 | 69 | |
| 70 | - $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count); |
|
| 70 | + $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | // public function testBelongsToManyMany() |
@@ -94,9 +94,9 @@ discard block |
||
| 94 | 94 | ProxyDBCounterExtension::resetQueries(); |
| 95 | 95 | $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount(); |
| 96 | 96 | |
| 97 | - $players = Player::get()->with(['Team','Origin']); |
|
| 97 | + $players = Player::get()->with(['Team', 'Origin']); |
|
| 98 | 98 | |
| 99 | - foreach($players as $player) { |
|
| 99 | + foreach ($players as $player) { |
|
| 100 | 100 | $team = $player->Team->Title; |
| 101 | 101 | $origin = $player->Origin->Title; |
| 102 | 102 | } |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | // print_r(ProxyDBCounterExtension::getQueries()); |
| 106 | 106 | |
| 107 | - $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count); |
|
| 107 | + $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | public function testHasOneChain() |
@@ -115,14 +115,14 @@ discard block |
||
| 115 | 115 | |
| 116 | 116 | $players = Player::get()->with(['Team.Origin']); |
| 117 | 117 | |
| 118 | - foreach($players as $player) { |
|
| 118 | + foreach ($players as $player) { |
|
| 119 | 119 | $team = $player->Team->Origin->Title; |
| 120 | 120 | } |
| 121 | 121 | $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); |
| 122 | 122 | |
| 123 | 123 | // print_r(ProxyDBCounterExtension::getQueries()); |
| 124 | 124 | |
| 125 | - $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count); |
|
| 125 | + $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | public function testChainOneMany() |
@@ -133,12 +133,12 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | $players = Player::get()->with(['Team.Players']); |
| 135 | 135 | |
| 136 | - foreach($players as $player) { |
|
| 136 | + foreach ($players as $player) { |
|
| 137 | 137 | $team = $player->Team->Players()->map()->toArray(); |
| 138 | 138 | } |
| 139 | 139 | $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); |
| 140 | 140 | |
| 141 | - $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count); |
|
| 141 | + $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | } |
@@ -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 | } |