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.

213 lines
5.7 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
{
/**
* @var ActiveRelation
*/
public $relation;
/**
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
*/
public $relatedChildren = array();
/**
* @var boolean whether this element is only for join purpose. If true, data will also be populated into the AR of this element.
*/
public $joinOnly;
13 years ago
public $columnAliases = array(); // alias => original name
public $pkAlias = array(); // original name => alias
public $records;
public $relatedRecords;
13 years ago
public function __construct($relation, $parent, $relatedParent)
13 years ago
{
13 years ago
$this->relation = $relation;
13 years ago
if ($parent !== null) {
$this->parent = $parent;
$parent->children[$relation->name] = $this;
13 years ago
$relatedParent->relatedChildren[$relation->name] = $this;
13 years ago
}
}
public function populateData($row)
{
$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);
// create active record
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;
}
}
$modelClass = $this->relation->modelClass;
13 years ago
$record = $modelClass::populateData($attributes);
13 years ago
foreach ($this->children as $child) {
if ($child->relation->select !== false) {
13 years ago
$record->initRelation($child->relation);
13 years ago
}
}
$this->records[$pk] = $record;
}
// populate child records
13 years ago
foreach ($this->relatedChildren as $child) {
if ($child->relation->select === false || $child->joinOnly) {
13 years ago
continue;
}
$childRecord = $child->populateData($row);
if ($childRecord === null) {
continue;
}
if ($child->relation->hasMany) {
$fpk = serialize($childRecord->getPrimaryKey());
if (isset($this->relatedRecords[$pk][$child->relation->name][$fpk])) {
continue;
}
$this->relatedRecords[$pk][$child->relation->name][$fpk] = true;
}
$record->addRelatedRecord($child->relation, $childRecord);
}
return $record;
}
public function buildQuery($query)
{
$tokens = array(
13 years ago
'@.' => $this->relation->tableAlias . '.',
'?.' => $this->parent->relation->tableAlias . '.',
13 years ago
);
13 years ago
foreach ($this->buildSelect($this->relation->select) as $column) {
13 years ago
$query->select[] = strtr($column, $tokens);
}
if ($this->relation->where !== null) {
$query->where[] = strtr($this->relation->where, $tokens);
}
if ($this->relation->having !== null) {
$query->having[] = strtr($this->relation->having, $tokens);
}
13 years ago
if ($this->relation->via !== null) {
$query->join[] = $this->relation->via;
}
$modelClass = $this->relation->modelClass;
$tableName = $modelClass::tableName();
$joinType = $this->relation->joinType === null ? 'LEFT JOIN' : $this->relation->joinType;
$join = "$joinType $tableName {$this->relation->tableAlias}";
if ($this->relation->on !== null) {
$join .= ' ON ' . strtr($this->relation->on, $tokens);
}
$query->join[] = $join;
if ($this->relation->join !== null) {
$query->join[] = strtr($this->relation->join, $tokens);
}
// todo: convert orderBy to array first
if ($this->relation->orderBy !== null) {
$query->orderBy[] = strtr($this->relation->orderBy, $tokens);
}
// todo: convert groupBy to array first
if ($this->relation->groupBy !== null) {
$query->groupBy[] = strtr($this->relation->groupBy, $tokens);
}
if ($this->relation->params !== null) {
foreach ($this->relation->params as $name => $value) {
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
{
$modelClass = $this->relation->modelClass;
$tableSchema = $modelClass::getMetaData()->table;
$columns = array();
$columnCount = 0;
13 years ago
$prefix = $this->relation->tableAlias;
13 years ago
if (empty($select) || $select === '*') {
foreach ($tableSchema->columns as $column) {
13 years ago
$alias = $this->relation->tableAlias . '_' . ($columnCount++);
$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);
}
foreach ($tableSchema->primaryKey as $column) {
13 years ago
$alias = $this->relation->tableAlias . '_' . ($columnCount++);
$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 = $this->relation->tableAlias . '_' . ($columnCount++);
$columns[] = "$prefix.$column AS $alias";
13 years ago
$this->columnAliases[$alias] = $column;
}
}
}
return $columns;
}
}