@@ -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 | $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 | $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 | $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 | $player->Team->Title; |
| 101 | 101 | $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 | $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 | $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,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 | } |