mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 21:41:19 +08:00
renamed indexBy to be index.
This commit is contained in:
@ -74,21 +74,21 @@ class ActiveFinder extends \yii\base\Object
|
|||||||
$rows = $command->queryAll();
|
$rows = $command->queryAll();
|
||||||
$records = array();
|
$records = array();
|
||||||
if ($query->asArray) {
|
if ($query->asArray) {
|
||||||
if ($query->indexBy === null) {
|
if ($query->index === null) {
|
||||||
return $rows;
|
return $rows;
|
||||||
}
|
}
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$records[$row[$query->indexBy]] = $row;
|
$records[$row[$query->index]] = $row;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$class = $query->modelClass;
|
$class = $query->modelClass;
|
||||||
if ($query->indexBy === null) {
|
if ($query->index === null) {
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$records[] = $class::create($row);
|
$records[] = $class::create($row);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$records[$row[$query->indexBy]] = $class::create($row);
|
$records[$row[$query->index]] = $class::create($row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,10 +125,10 @@ class ActiveFinder extends \yii\base\Object
|
|||||||
$joinTree->createRecord($row);
|
$joinTree->createRecord($row);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($query->indexBy !== null) {
|
if ($query->index !== null) {
|
||||||
$records = array();
|
$records = array();
|
||||||
foreach ($joinTree->records as $record) {
|
foreach ($joinTree->records as $record) {
|
||||||
$records[$record[$query->indexBy]] = $record;
|
$records[$record[$query->index]] = $record;
|
||||||
}
|
}
|
||||||
return $records;
|
return $records;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -524,8 +524,8 @@ abstract class ActiveRecord extends Model
|
|||||||
public function addRelatedRecord($relation, $record)
|
public function addRelatedRecord($relation, $record)
|
||||||
{
|
{
|
||||||
if ($relation->hasMany) {
|
if ($relation->hasMany) {
|
||||||
if ($relation->indexBy !== null) {
|
if ($relation->index !== null) {
|
||||||
$this->_related[$relation->name][$record->{$relation->indexBy}] = $record;
|
$this->_related[$relation->name][$record->{$relation->index}] = $record;
|
||||||
} else {
|
} else {
|
||||||
$this->_related[$relation->name][] = $record;
|
$this->_related[$relation->name][] = $record;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,7 +34,7 @@ class BaseActiveQuery extends BaseQuery
|
|||||||
* @var string the name of the column that the result should be indexed by.
|
* @var string the name of the column that the result should be indexed by.
|
||||||
* This is only useful when the query result is returned as an array.
|
* This is only useful when the query result is returned as an array.
|
||||||
*/
|
*/
|
||||||
public $indexBy;
|
public $index;
|
||||||
/**
|
/**
|
||||||
* @var boolean whether to return each record as an array. If false (default), an object
|
* @var boolean whether to return each record as an array. If false (default), an object
|
||||||
* of [[modelClass]] will be created to represent each record.
|
* of [[modelClass]] will be created to represent each record.
|
||||||
@ -61,9 +61,9 @@ class BaseActiveQuery extends BaseQuery
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function indexBy($column)
|
public function index($column)
|
||||||
{
|
{
|
||||||
$this->indexBy = $column;
|
$this->index = $column;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -111,8 +111,8 @@ class JoinElement extends \yii\base\Object
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ($child->query->hasMany) {
|
if ($child->query->hasMany) {
|
||||||
if ($child->query->indexBy !== null) {
|
if ($child->query->index !== null) {
|
||||||
$hash = $childRecord[$child->query->indexBy];
|
$hash = $childRecord[$child->query->index];
|
||||||
} else {
|
} else {
|
||||||
$hash = serialize($childRecord->getPrimaryKey());
|
$hash = serialize($childRecord->getPrimaryKey());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -217,8 +217,8 @@ class ActiveRecordTest extends \yiiunit\MysqlTestCase
|
|||||||
$customers = Customer::find()->order('id')->asArray()->all();
|
$customers = Customer::find()->order('id')->asArray()->all();
|
||||||
$this->assertEquals('user2', $customers[1]['name']);
|
$this->assertEquals('user2', $customers[1]['name']);
|
||||||
|
|
||||||
// indexBy
|
// index
|
||||||
$customers = Customer::find()->order('id')->indexBy('name')->all();
|
$customers = Customer::find()->order('id')->index('name')->all();
|
||||||
$this->assertEquals(2, $customers['user2']['id']);
|
$this->assertEquals(2, $customers['user2']['id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user