Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.0 KiB

13 years ago
<?php
/**
13 years ago
* JoinElement class file.
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db\ar;
use yii\base\VectorIterator;
use yii\db\dao\Query;
use yii\db\Exception;
class JoinElement extends \yii\base\Object
{
/**
13 years ago
* @var integer ID of this join element
13 years ago
*/
13 years ago
public $id;
/**
* @var BaseActiveQuery
*/
public $query;
13 years ago
/**
13 years ago
* @var JoinElement the parent element that this element needs to join with
13 years ago
*/
public $parent;
/**
13 years ago
* @var JoinElement[] the child elements that need to join with this element
13 years ago
*/
public $children = array();
13 years ago
/**
* @var JoinElement[] the child elements that have relations declared in the AR class of this element
*/
13 years ago
public $relations = array();
13 years ago
/**
13 years ago
* @var boolean whether this element is only for join purpose. If false, data will also be populated into the AR of this element.
13 years ago
*/
public $joinOnly;
13 years ago
public $columnAliases = array(); // alias => original name
public $pkAlias = array(); // original name => alias
public $records;
public $relatedRecords;
13 years ago
/**
* @param ActiveRelation|ActiveQuery $query
* @param JoinElement $parent
* @param JoinElement $container
*/
public function __construct($id, $query, $parent, $container)
13 years ago
{
13 years ago
$this->id = $id;
$this->query = $query;
13 years ago
if ($parent !== null) {
$this->parent = $parent;
13 years ago
$parent->children[$query->name] = $this;
$container->relations[$query->name] = $this;
13 years ago
}
}
13 years ago
/**
* @param array $row
* @return null|ActiveRecord
*/
public function createRecord($row)
13 years ago
{
$pk = array();
foreach ($this->pkAlias as $alias) {
if (isset($row[$alias])) {
$pk[] = $row[$alias];
} else {
return null;
}
}
$pk = count($pk) === 1 ? $pk[0] : serialize($pk);
13 years ago
// create record
13 years ago
if (isset($this->records[$pk])) {
$record = $this->records[$pk];
} else {
$attributes = array();
foreach ($row as $alias => $value) {
if (isset($this->columnAliases[$alias])) {
$attributes[$this->columnAliases[$alias]] = $value;
}
}
13 years ago
$modelClass = $this->query->modelClass;
$this->records[$pk] = $record = $modelClass::create($attributes);
13 years ago
foreach ($this->children as $child) {
13 years ago
if ($child->query->select !== false || $child->joinOnly) {
$record->initRelation($child->query);
13 years ago
}
}
}
13 years ago
// add related records
foreach ($this->relations as $child) {
if ($child->query->select === false || $child->joinOnly) {
13 years ago
continue;
}
13 years ago
$childRecord = $child->createRecord($row);
13 years ago
if ($childRecord === null) {
continue;
}
13 years ago
if ($child->query->hasMany) {
if ($child->query->indexBy !== null) {
$hash = $childRecord->{$child->query->indexBy};
} else {
$hash = serialize($childRecord->getPrimaryKey());
}
if (!isset($this->relatedRecords[$pk][$child->query->name][$hash])) {
$this->relatedRecords[$pk][$child->query->name][$hash] = true;
$record->addRelatedRecord($child->query, $childRecord);
13 years ago
}
13 years ago
} else {
$record->addRelatedRecord($child->query, $childRecord);
13 years ago
}
}
return $record;
}
public function buildQuery($query)
{
13 years ago
$prefixes = array(
'@.' => $this->query->tableAlias . '.',
'?.' => $this->parent->query->tableAlias . '.',
13 years ago
);
13 years ago
$quotedPrefixes = '';
foreach ($this->buildSelect($this->query->select) as $column) {
$query->select[] = strtr($column, $prefixes);
13 years ago
}
13 years ago
if ($this->query->where !== null) {
$query->where[] = strtr($this->query->where, $prefixes);
13 years ago
}
13 years ago
if ($this->query->having !== null) {
$query->having[] = strtr($this->query->having, $prefixes);
13 years ago
}
13 years ago
if ($this->query->via !== null) {
$query->join[] = $this->query->via;
13 years ago
}
13 years ago
$modelClass = $this->query->modelClass;
13 years ago
$tableName = $modelClass::tableName();
13 years ago
$joinType = $this->query->joinType === null ? 'LEFT JOIN' : $this->query->joinType;
$join = "$joinType $tableName {$this->query->tableAlias}";
if ($this->query->on !== null) {
$join .= ' ON ' . strtr($this->query->on, $prefixes);
13 years ago
}
$query->join[] = $join;
13 years ago
if ($this->query->join !== null) {
$query->join[] = strtr($this->query->join, $prefixes);
13 years ago
}
// todo: convert orderBy to array first
13 years ago
if ($this->query->orderBy !== null) {
$query->orderBy[] = strtr($this->query->orderBy, $prefixes);
13 years ago
}
// todo: convert groupBy to array first
13 years ago
if ($this->query->groupBy !== null) {
$query->groupBy[] = strtr($this->query->groupBy, $prefixes);
13 years ago
}
13 years ago
if ($this->query->params !== null) {
foreach ($this->query->params as $name => $value) {
13 years ago
if (is_integer($name)) {
$query->params[] = $value;
} else {
$query->params[$name] = $value;
}
}
}
13 years ago
foreach ($this->children as $child) {
$child->buildQuery($query);
}
}
13 years ago
public function buildSelect($select)
13 years ago
{
13 years ago
if ($select === false) {
return array();
}
$modelClass = $this->query->modelClass;
$table = $modelClass::getMetaData()->table;
13 years ago
$columns = array();
$columnCount = 0;
13 years ago
$prefix = $this->query->tableAlias;
13 years ago
if (empty($select) || $select === '*') {
13 years ago
foreach ($table->columns as $column) {
$alias = "t{$this->id}c" . ($columnCount++);
13 years ago
$columns[] = "$prefix.{$column->name} AS $alias";
13 years ago
$this->columnAliases[$alias] = $column->name;
if ($column->isPrimaryKey) {
$this->pkAlias[$column->name] = $alias;
}
}
} else {
if (is_string($select)) {
$select = explode(',', $select);
}
13 years ago
foreach ($table->primaryKey as $column) {
$alias = "t{$this->id}c" . ($columnCount++);
13 years ago
$columns[] = "$prefix.$column AS $alias";
13 years ago
$this->pkAlias[$column] = $alias;
}
foreach ($select as $column) {
$column = trim($column);
if (preg_match('/^(.*?)\s+AS\s+(\w+)$/im', $column, $matches)) {
// if the column is already aliased
$this->columnAliases[$matches[2]] = $matches[2];
$columns[] = $column;
} elseif (!isset($this->pkAlias[$column])) {
13 years ago
$alias = "t{$this->id}c" . ($columnCount++);
13 years ago
$columns[] = "$prefix.$column AS $alias";
13 years ago
$this->columnAliases[$alias] = $column;
}
}
}
return $columns;
}
}