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.

173 lines
4.8 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
13 years ago
{
/**
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
public $container;
13 years ago
/**
13 years ago
* @var JoinElement[] the child elements that need to join with this element
13 years ago
*/
public $children = array();
13 years ago
/**
13 years ago
* @var JoinElement[] the child elements that have corresponding relations declared in the AR class of this element
13 years ago
*/
13 years ago
public $relations = array();
13 years ago
/**
13 years ago
* @var array column aliases (alias => original name)
*/
public $columnAliases = array();
/**
* @var array primary key column aliases (original name => alias)
*/
public $pkAlias = array();
/**
* @var string|array the column(s) used for index the query results
*/
public $key;
/**
* @var array query results for this element (PK value => AR instance or data array)
*/
public $records = array();
public $related = array();
13 years ago
13 years ago
/**
13 years ago
* @param integer $id
13 years ago
* @param ActiveRelation|ActiveQuery $query
13 years ago
* @param null|JoinElement $parent
* @param null|JoinElement $container
13 years ago
*/
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
$this->container = $container;
13 years ago
$parent->children[$query->name] = $this;
13 years ago
if ($query->select !== false) {
$container->relations[$query->name] = $this;
}
13 years ago
}
}
13 years ago
public function populateData($rows)
13 years ago
{
13 years ago
if ($this->container === null) {
foreach ($rows as $row) {
if (($key = $this->getKeyValue($row)) !== null && !isset($this->records[$key])) {
$this->records[$key] = $this->createRecord($row);
}
13 years ago
}
} else {
13 years ago
foreach ($rows as $row) {
$key = $this->getKeyValue($row);
$containerKey = $this->container->getKeyValue($row);
if ($key === null || $containerKey === null || isset($this->related[$containerKey][$key])) {
continue;
13 years ago
}
13 years ago
$this->related[$containerKey][$key] = true;
if ($this->query->asArray) {
if (isset($this->records[$key])) {
if ($this->query->hasMany) {
if ($this->query->index !== null) {
$this->container->records[$containerKey][$this->query->name][$key] =& $this->records[$key];
} else {
$this->container->records[$containerKey][$this->query->name][] =& $this->records[$key];
}
} else {
$this->container->records[$containerKey][$this->query->name] =& $this->records[$key];
}
} else {
$record = $this->createRecord($row);
if ($this->query->hasMany) {
if ($this->query->index !== null) {
$this->container->records[$containerKey][$this->query->name][$key] = $record;
$this->records[$key] =& $this->container->records[$containerKey][$this->query->name][$key];
} else {
$count = count($this->container->records[$containerKey][$this->query->name]);
$this->container->records[$containerKey][$this->query->name][] = $record;
$this->records[$key] =& $this->container->records[$containerKey][$this->query->name][$count];
}
} else {
$this->container->records[$containerKey][$this->query->name] = $record;
$this->records[$key] =& $this->container->records[$containerKey][$this->query->name];
}
}
} else {
if (isset($this->records[$key])) {
$record = $this->records[$key];
} else {
$this->records[$key] = $record = $this->createRecord($row);
}
$this->container->records[$containerKey]->addRelatedRecord($this->query, $record);
13 years ago
}
}
}
13 years ago
foreach ($this->relations as $child) {
13 years ago
$child->populateData($rows);
}
}
protected function getKeyValue($row)
{
if (is_array($this->key)) {
$key = array();
foreach ($this->key as $alias) {
if (!isset($row[$alias])) {
return null;
13 years ago
}
13 years ago
$key[] = $row[$alias];
13 years ago
}
13 years ago
return serialize($key);
} else {
return $row[$this->key];
13 years ago
}
13 years ago
}
13 years ago
13 years ago
protected function createRecord($row)
{
$record = array();
foreach ($this->columnAliases as $alias => $name) {
$record[$name] = $row[$alias];
}
if ($this->query->asArray) {
foreach ($this->relations as $child) {
$record[$child->query->name] = $child->query->hasMany ? array() : null;
}
} else {
$modelClass = $this->query->modelClass;
$record = $modelClass::create($record);
foreach ($this->relations as $child) {
$record->{$child->query->name} = $child->query->hasMany ? array() : null;
}
}
13 years ago
return $record;
}
}