Commit 62336b43 by Qiang Xue

...

parent 50743935
...@@ -91,8 +91,8 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA ...@@ -91,8 +91,8 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA
public function one() public function one()
{ {
if ($this->records === null) { if ($this->records === null) {
// todo: load only one record $this->limit = 1;
$this->records = $this->findRecords(false); $this->records = $this->findRecords();
} }
return isset($this->records[0]) ? $this->records[0] : null; return isset($this->records[0]) ? $this->records[0] : null;
} }
...@@ -240,13 +240,13 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA ...@@ -240,13 +240,13 @@ class ActiveQuery extends BaseActiveQuery implements \IteratorAggregate, \ArrayA
unset($this->records[$offset]); unset($this->records[$offset]);
} }
protected function findRecords($all = true) protected function findRecords()
{ {
$finder = new ActiveFinder($this->getDbConnection()); $finder = new ActiveFinder($this->getDbConnection());
if (!empty($this->with)) { if (!empty($this->with)) {
return $finder->findRecordsWithRelations(); return $finder->findRecordsWithRelations($this);
} else { } else {
return $finder->findRecords($this, $all); return $finder->findRecords($this);
} }
} }
} }
...@@ -517,10 +517,18 @@ abstract class ActiveRecord extends Model ...@@ -517,10 +517,18 @@ abstract class ActiveRecord extends Model
$this->_related[$relation->name] = $relation->hasMany ? array() : null; $this->_related[$relation->name] = $relation->hasMany ? array() : null;
} }
/**
* @param ActiveRelation $relation
* @param ActiveRecord $record
*/
public function addRelatedRecord($relation, $record) public function addRelatedRecord($relation, $record)
{ {
if ($relation->hasMany) { if ($relation->hasMany) {
$this->_related[$relation->name][] = $record; if ($relation->indexBy !== null) {
$this->_related[$relation->name][$record->{$relation->indexBy}] = $record;
} else {
$this->_related[$relation->name][] = $record;
}
} else { } else {
$this->_related[$relation->name] = $record; $this->_related[$relation->name] = $record;
} }
...@@ -1027,12 +1035,10 @@ abstract class ActiveRecord extends Model ...@@ -1027,12 +1035,10 @@ abstract class ActiveRecord extends Model
/** /**
* Creates an active record with the given attributes. * Creates an active record with the given attributes.
* This method is internally used by the find methods. * @param array $row attribute values (name => value)
* @param array $row attribute values (column name=>column value) * @return ActiveRecord the newly created active record.
* @return ActiveRecord the newly created active record. The class of the object is the same as the model class.
* Null is returned if the input data is false.
*/ */
public static function createRecord($row) public static function create($row)
{ {
$record = static::instantiate($row); $record = static::instantiate($row);
$columns = static::getMetaData()->table->columns; $columns = static::getMetaData()->table->columns;
......
...@@ -18,9 +18,13 @@ use yii\db\Exception; ...@@ -18,9 +18,13 @@ use yii\db\Exception;
class JoinElement extends \yii\base\Object class JoinElement extends \yii\base\Object
{ {
/** /**
* @var ActiveRelation * @var integer ID of this join element
*/ */
public $relation; public $id;
/**
* @var BaseActiveQuery
*/
public $query;
/** /**
* @var JoinElement the parent element that this element needs to join with * @var JoinElement the parent element that this element needs to join with
*/ */
...@@ -32,9 +36,9 @@ class JoinElement extends \yii\base\Object ...@@ -32,9 +36,9 @@ class JoinElement extends \yii\base\Object
/** /**
* @var JoinElement[] the child elements that have relations declared in the AR class of this element * @var JoinElement[] the child elements that have relations declared in the AR class of this element
*/ */
public $relatedChildren = array(); public $relations = array();
/** /**
* @var boolean whether this element is only for join purpose. If true, data will also be populated into the AR of this element. * @var boolean whether this element is only for join purpose. If false, data will also be populated into the AR of this element.
*/ */
public $joinOnly; public $joinOnly;
...@@ -44,17 +48,27 @@ class JoinElement extends \yii\base\Object ...@@ -44,17 +48,27 @@ class JoinElement extends \yii\base\Object
public $records; public $records;
public $relatedRecords; public $relatedRecords;
public function __construct($relation, $parent, $relatedParent) /**
* @param ActiveRelation|ActiveQuery $query
* @param JoinElement $parent
* @param JoinElement $container
*/
public function __construct($id, $query, $parent, $container)
{ {
$this->relation = $relation; $this->id = $id;
$this->query = $query;
if ($parent !== null) { if ($parent !== null) {
$this->parent = $parent; $this->parent = $parent;
$parent->children[$relation->name] = $this; $parent->children[$query->name] = $this;
$relatedParent->relatedChildren[$relation->name] = $this; $container->relations[$query->name] = $this;
} }
} }
public function populateData($row) /**
* @param array $row
* @return null|ActiveRecord
*/
public function createRecord($row)
{ {
$pk = array(); $pk = array();
foreach ($this->pkAlias as $alias) { foreach ($this->pkAlias as $alias) {
...@@ -66,7 +80,7 @@ class JoinElement extends \yii\base\Object ...@@ -66,7 +80,7 @@ class JoinElement extends \yii\base\Object
} }
$pk = count($pk) === 1 ? $pk[0] : serialize($pk); $pk = count($pk) === 1 ? $pk[0] : serialize($pk);
// create active record // create record
if (isset($this->records[$pk])) { if (isset($this->records[$pk])) {
$record = $this->records[$pk]; $record = $this->records[$pk];
} else { } else {
...@@ -76,33 +90,37 @@ class JoinElement extends \yii\base\Object ...@@ -76,33 +90,37 @@ class JoinElement extends \yii\base\Object
$attributes[$this->columnAliases[$alias]] = $value; $attributes[$this->columnAliases[$alias]] = $value;
} }
} }
$modelClass = $this->relation->modelClass; $modelClass = $this->query->modelClass;
$record = $modelClass::populateData($attributes); $this->records[$pk] = $record = $modelClass::create($attributes);
foreach ($this->children as $child) { foreach ($this->children as $child) {
if ($child->relation->select !== false) { if ($child->query->select !== false || $child->joinOnly) {
$record->initRelation($child->relation); $record->initRelation($child->query);
} }
} }
$this->records[$pk] = $record;
} }
// populate child records // add related records
foreach ($this->relatedChildren as $child) { foreach ($this->relations as $child) {
if ($child->relation->select === false || $child->joinOnly) { if ($child->query->select === false || $child->joinOnly) {
continue; continue;
} }
$childRecord = $child->populateData($row); $childRecord = $child->createRecord($row);
if ($childRecord === null) { if ($childRecord === null) {
continue; continue;
} }
if ($child->relation->hasMany) { if ($child->query->hasMany) {
$fpk = serialize($childRecord->getPrimaryKey()); if ($child->query->indexBy !== null) {
if (isset($this->relatedRecords[$pk][$child->relation->name][$fpk])) { $hash = $childRecord->{$child->query->indexBy};
continue; } 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);
} }
$this->relatedRecords[$pk][$child->relation->name][$fpk] = true; } else {
$record->addRelatedRecord($child->query, $childRecord);
} }
$record->addRelatedRecord($child->relation, $childRecord);
} }
return $record; return $record;
...@@ -110,52 +128,53 @@ class JoinElement extends \yii\base\Object ...@@ -110,52 +128,53 @@ class JoinElement extends \yii\base\Object
public function buildQuery($query) public function buildQuery($query)
{ {
$tokens = array( $prefixes = array(
'@.' => $this->relation->tableAlias . '.', '@.' => $this->query->tableAlias . '.',
'?.' => $this->parent->relation->tableAlias . '.', '?.' => $this->parent->query->tableAlias . '.',
); );
foreach ($this->buildSelect($this->relation->select) as $column) { $quotedPrefixes = '';
$query->select[] = strtr($column, $tokens); foreach ($this->buildSelect($this->query->select) as $column) {
$query->select[] = strtr($column, $prefixes);
} }
if ($this->relation->where !== null) { if ($this->query->where !== null) {
$query->where[] = strtr($this->relation->where, $tokens); $query->where[] = strtr($this->query->where, $prefixes);
} }
if ($this->relation->having !== null) { if ($this->query->having !== null) {
$query->having[] = strtr($this->relation->having, $tokens); $query->having[] = strtr($this->query->having, $prefixes);
} }
if ($this->relation->via !== null) { if ($this->query->via !== null) {
$query->join[] = $this->relation->via; $query->join[] = $this->query->via;
} }
$modelClass = $this->relation->modelClass; $modelClass = $this->query->modelClass;
$tableName = $modelClass::tableName(); $tableName = $modelClass::tableName();
$joinType = $this->relation->joinType === null ? 'LEFT JOIN' : $this->relation->joinType; $joinType = $this->query->joinType === null ? 'LEFT JOIN' : $this->query->joinType;
$join = "$joinType $tableName {$this->relation->tableAlias}"; $join = "$joinType $tableName {$this->query->tableAlias}";
if ($this->relation->on !== null) { if ($this->query->on !== null) {
$join .= ' ON ' . strtr($this->relation->on, $tokens); $join .= ' ON ' . strtr($this->query->on, $prefixes);
} }
$query->join[] = $join; $query->join[] = $join;
if ($this->relation->join !== null) { if ($this->query->join !== null) {
$query->join[] = strtr($this->relation->join, $tokens); $query->join[] = strtr($this->query->join, $prefixes);
} }
// todo: convert orderBy to array first // todo: convert orderBy to array first
if ($this->relation->orderBy !== null) { if ($this->query->orderBy !== null) {
$query->orderBy[] = strtr($this->relation->orderBy, $tokens); $query->orderBy[] = strtr($this->query->orderBy, $prefixes);
} }
// todo: convert groupBy to array first // todo: convert groupBy to array first
if ($this->relation->groupBy !== null) { if ($this->query->groupBy !== null) {
$query->groupBy[] = strtr($this->relation->groupBy, $tokens); $query->groupBy[] = strtr($this->query->groupBy, $prefixes);
} }
if ($this->relation->params !== null) { if ($this->query->params !== null) {
foreach ($this->relation->params as $name => $value) { foreach ($this->query->params as $name => $value) {
if (is_integer($name)) { if (is_integer($name)) {
$query->params[] = $value; $query->params[] = $value;
} else { } else {
...@@ -171,14 +190,17 @@ class JoinElement extends \yii\base\Object ...@@ -171,14 +190,17 @@ class JoinElement extends \yii\base\Object
public function buildSelect($select) public function buildSelect($select)
{ {
$modelClass = $this->relation->modelClass; if ($select === false) {
$tableSchema = $modelClass::getMetaData()->table; return array();
}
$modelClass = $this->query->modelClass;
$table = $modelClass::getMetaData()->table;
$columns = array(); $columns = array();
$columnCount = 0; $columnCount = 0;
$prefix = $this->relation->tableAlias; $prefix = $this->query->tableAlias;
if (empty($select) || $select === '*') { if (empty($select) || $select === '*') {
foreach ($tableSchema->columns as $column) { foreach ($table->columns as $column) {
$alias = $this->relation->tableAlias . '_' . ($columnCount++); $alias = "t{$this->id}c" . ($columnCount++);
$columns[] = "$prefix.{$column->name} AS $alias"; $columns[] = "$prefix.{$column->name} AS $alias";
$this->columnAliases[$alias] = $column->name; $this->columnAliases[$alias] = $column->name;
if ($column->isPrimaryKey) { if ($column->isPrimaryKey) {
...@@ -189,8 +211,8 @@ class JoinElement extends \yii\base\Object ...@@ -189,8 +211,8 @@ class JoinElement extends \yii\base\Object
if (is_string($select)) { if (is_string($select)) {
$select = explode(',', $select); $select = explode(',', $select);
} }
foreach ($tableSchema->primaryKey as $column) { foreach ($table->primaryKey as $column) {
$alias = $this->relation->tableAlias . '_' . ($columnCount++); $alias = "t{$this->id}c" . ($columnCount++);
$columns[] = "$prefix.$column AS $alias"; $columns[] = "$prefix.$column AS $alias";
$this->pkAlias[$column] = $alias; $this->pkAlias[$column] = $alias;
} }
...@@ -201,7 +223,7 @@ class JoinElement extends \yii\base\Object ...@@ -201,7 +223,7 @@ class JoinElement extends \yii\base\Object
$this->columnAliases[$matches[2]] = $matches[2]; $this->columnAliases[$matches[2]] = $matches[2];
$columns[] = $column; $columns[] = $column;
} elseif (!isset($this->pkAlias[$column])) { } elseif (!isset($this->pkAlias[$column])) {
$alias = $this->relation->tableAlias . '_' . ($columnCount++); $alias = "t{$this->id}c" . ($columnCount++);
$columns[] = "$prefix.$column AS $alias"; $columns[] = "$prefix.$column AS $alias";
$this->columnAliases[$alias] = $column; $this->columnAliases[$alias] = $column;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment