Esempio n. 1
0
 public function buildSelect()
 {
     $this->selectIsDirty = \false;
     $select = $this->table->select($this->table)->setIntegrityCheck(\false);
     $tableInfo = $this->table->info();
     $columns = $tableInfo['cols'];
     foreach ($this->table->getDependentTables() as $dependentTable) {
         $dependentTableInstance = $this->table->getDependentTableInstance($dependentTable);
         $dependentTableInfo = $dependentTableInstance->info();
         $columns = array_merge($columns, $dependentTableInfo['cols']);
         $reference = $dependentTableInstance->getReference(get_class($this->table));
         $onCondition = [];
         foreach ($reference['columns'] as $k => $column) {
             $refColumn = $reference['refColumns'][$k];
             $onCondition[] = $this->table->getAdapter()->quoteIdentifier($tableInfo['name']) . '.' . $this->table->getAdapter()->quoteIdentifier($refColumn) . ' = ' . $this->table->getAdapter()->quoteIdentifier($dependentTableInfo['name']) . '.' . $this->table->getAdapter()->quoteIdentifier($column);
         }
         if (empty($onCondition)) {
             throw new \Exception('Cannot join tables', 500);
         }
         $dependentTableAlias = $dependentTableInfo['name'];
         if (isset($this->join[$dependentTableAlias])) {
             foreach ($this->join[$dependentTableAlias] as $condition) {
                 $onCondition[] = $this->table->getAdapter()->quoteIdentifier($dependentTableAlias) . "." . $this->table->getAdapter()->quoteIdentifier($condition[0]) . " = " . $this->table->getAdapter()->quote($condition[1]);
             }
         }
         $joinType = 'joinLeft';
         if (isset($reference['joinType'])) {
             switch ($reference['joinType']) {
                 case 'inner':
                     $joinType = 'joinInner';
                     break;
             }
         }
         $select->{$joinType}($dependentTableInfo['name'], implode(" AND ", $onCondition), array_diff($dependentTableInfo['cols'], $tableInfo['cols']));
     }
     $this->applyWhereConditions($select);
     foreach ($this->order as $condition) {
         $select->order((string) $condition);
     }
     return $select;
 }