Example #1
0
 protected function _constructJoins($sourceTable = null)
 {
     if ($sourceTable === null) {
         $this->getStorage()->registerForeignKeys();
         $this->_join = array();
         // reset joins
         $sourceTable = $this->_storageSource;
         $this->_alreadyJoined = array();
     }
     $rootId = $this->_storageSource->getUniqueId();
     $sourceId = $sourceTable->getUniqueId();
     $this->_alreadyJoined[$sourceId] = true;
     //$allJoins = array(); // [target][source] = joinId
     //$joinContent = array();
     //$joinId = 0;
     // JOIN WITH
     if (isset($this->_joinWith[$sourceId])) {
         foreach ($this->_joinWith[$sourceId] as $targetId) {
             $targetTable = modelCollection::getInstanceById($targetId);
             $joins = modelStorage::getIndirectTablesJoins($sourceTable, $targetTable, $this->_joinType, $this->_joinWhere);
             foreach ($joins as $id => $joinString) {
                 $this->_join[$id] = $joinString;
                 $this->_alreadyJoined[$id] = true;
             }
             $this->_alreadyJoined[$targetId] = true;
             unset($this->_joinWith[$targetId]);
         }
     }
     // JOIN OTHER
     foreach ($this->_joinedTables as $targetId => $targetTable) {
         if ($rootId !== $targetId) {
             // && (!$alreadyJoined[$targetId]
             $min = null;
             $minJoins = false;
             // Trying to join table
             //echo '<div><b>'.$sourceTable->getTableName().' JOIN '.$table2->getTableName().'</b></div>';
             foreach ($this->_joinedTables as $xSourceId => $sourceTable) {
                 if ($xSourceId !== $targetId) {
                     $joins = modelStorage::getIndirectTablesJoins($sourceTable, $targetTable, $this->_joinType, $this->_joinWhere);
                     if ($joins !== false && ($min === null || count($joins) < $min)) {
                         $minJoins = $joins;
                         $min = count($joins);
                     }
                 }
             }
             if ($minJoins !== false) {
                 foreach ($minJoins as $id => $joinString) {
                     $this->_join[$id] = $joinString;
                     $this->_alreadyJoined[$id] = true;
                 }
             }
         }
     }
     //$this->_orderJoins($allJoins, $joinContent, $rootTableId);
 }