Beispiel #1
0
 /**
  * @param Driver $driver
  * @param Table $table
  * @param Query $query
  * @param bool $asArray
  */
 public function __construct(Driver $driver, Table $table, Query $query, $asArray = false)
 {
     $this->asArray = $asArray;
     $this->table = $table;
     $params = [];
     $this->tableName = $table->getName();
     if (!$this->asArray) {
         $this->recordClasses = [$this->tableName => $table->getDatabase()->getClassMapper()->getClassForRecord($this->tableName)];
     }
     $sql = $query->getRawSql();
     if ($sql === null) {
         $sql = $driver->buildSQL($table, $query, $params);
         $this->fetchStyle = \PDO::FETCH_NUM;
         foreach ($table->getColumns() as $column) {
             if ($column->isPrimaryKey()) {
                 $this->primaryKeyOrdinals[$this->tableName][$column->getOrdinal()] = true;
             }
             $this->columnOrdinalMap[$this->tableName][$column->getOrdinal()] = $column->getName();
         }
         $offset = count($table->getColumns());
         foreach ($query->getJoins() as $join) {
             if (isset($join['cardinality'])) {
                 $joinedTableName = $join['table'];
                 $joinedTable = $table->getDatabase()->getTable($joinedTableName);
                 $this->joinedTables[$joinedTableName] = $joinedTable;
                 if (!$this->asArray) {
                     $this->recordClasses[$joinedTableName] = $joinedTable->getDatabase()->getClassMapper()->getClassForRecord($joinedTableName);
                 }
                 foreach ($joinedTable->getColumns() as $column) {
                     if ($column->isPrimaryKey()) {
                         $this->primaryKeyOrdinals[$joinedTableName][$offset + $column->getOrdinal()] = true;
                     }
                     $this->columnOrdinalMap[$joinedTableName][$offset + $column->getOrdinal()] = $column->getName();
                 }
                 $this->cardinalities[$joinedTableName] = $join['cardinality'];
                 if ($join['cardinality'] === Query::CARDINALITY_ONE_TO_MANY) {
                     $this->properties[$joinedTableName] = $joinedTableName;
                 } else {
                     $this->properties[$joinedTableName] = array_keys($join['on']);
                 }
                 $offset += count($joinedTable->getColumns());
             }
         }
     } else {
         $this->fetchStyle = \PDO::FETCH_ASSOC;
         $this->rawSql = true;
     }
     $this->result = $driver->query($sql, $params);
     $this->fetchNextRow();
 }