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.

130 lines
3.1 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
/**
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
$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];
13 years ago
} else {
return null;
13 years ago
}
}
$pk = count($pk) === 1 ? $pk[0] : serialize($pk);
13 years ago
13 years ago
// create record
// todo: asArray
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->index !== null) {
$hash = $childRecord[$child->query->index];
13 years ago
} 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;
}
}