mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-12 11:40:19 +08:00
Correct #18499
This commit is contained in:
@@ -246,13 +246,26 @@ class Query extends Component implements QueryInterface, ExpressionInterface
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_string($this->indexBy) && $this->indexBy && is_array($this->select) && !in_array($this->indexBy, $this->select)) {
|
if (is_string($this->indexBy) && $this->indexBy && is_array($this->select)) {
|
||||||
if (strpos($this->indexBy, '.') === false && count($tables = $this->getTablesUsedInFrom()) > 0) {
|
$isIndexByAnArray = false;
|
||||||
$this->select[] = key($tables) . '.' . $this->indexBy;
|
if (strpos($this->indexBy, '.')) {
|
||||||
} else {
|
$indexByParts = explode('.', $this->indexBy);
|
||||||
$this->select[] = $this->indexBy;
|
foreach ($indexByParts as $indexByPart) {
|
||||||
|
if (is_numeric($indexByPart)) {
|
||||||
|
$isIndexByAnArray = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!$isIndexByAnArray && !in_array($this->indexBy, $this->select, true)) {
|
||||||
|
if (strpos($this->indexBy, '.') === false && count($tables = $this->getTablesUsedInFrom()) > 0) {
|
||||||
|
$this->select[] = key($tables) . '.' . $this->indexBy;
|
||||||
|
} else {
|
||||||
|
$this->select[] = $this->indexBy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$rows = $this->createCommand($db)->queryAll();
|
$rows = $this->createCommand($db)->queryAll();
|
||||||
|
|
||||||
return $this->populate($rows);
|
return $this->populate($rows);
|
||||||
|
|||||||
@@ -2095,4 +2095,25 @@ abstract class ActiveRecordTest extends DatabaseTestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://github.com/yiisoft/yii2/issues/18525
|
||||||
|
*/
|
||||||
|
public function testHasManyWithIndexBy()
|
||||||
|
{
|
||||||
|
$category = Category::find()->joinWith('items')->indexBy('items.0.name');
|
||||||
|
$this->assertEquals(['Agile Web Application Development with Yii1.1 and PHP5', 'Ice Age'], array_keys($category->all()));
|
||||||
|
|
||||||
|
$category = Category::find()->select([Category::tableName() . '.*'])->joinWith('items')->indexBy('items.0.name');
|
||||||
|
$this->assertEquals(['Agile Web Application Development with Yii1.1 and PHP5', 'Ice Age'], array_keys($category->all()));
|
||||||
|
|
||||||
|
$category = Category::find()->select([Category::tableName() . '.*'])->joinWith('items')->indexBy('name');
|
||||||
|
$this->assertEquals(['Books', 'Movies'], array_keys($category->all()));
|
||||||
|
|
||||||
|
$category = Category::find()->joinWith('items')->indexBy('item.name');
|
||||||
|
$this->assertEquals([''], array_keys($category->all()));
|
||||||
|
|
||||||
|
$category = Category::find()->select([Category::tableName() . '.name'])->joinWith('items')->indexBy('id');
|
||||||
|
$this->assertEquals([1, 2], array_keys($category->all()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user