Commit cc38e9c7 by Carsten Brandt

fixed issue with indexBy and array valued relations

parent d5375e8c
......@@ -62,6 +62,7 @@ trait ActiveRelationTrait
*/
public $inverseOf;
/**
* Clones internal objects.
*/
......@@ -106,7 +107,6 @@ trait ActiveRelationTrait
if ($callable !== null) {
call_user_func($callable, $relation);
}
return $this;
}
......@@ -133,7 +133,6 @@ trait ActiveRelationTrait
public function inverseOf($relationName)
{
$this->inverseOf = $relationName;
return $this;
}
......@@ -244,7 +243,14 @@ trait ActiveRelationTrait
$value = [];
foreach ($keys as $key) {
if (isset($buckets[$key])) {
$value = array_merge($value, $buckets[$key]);
if ($this->indexBy !== null) {
// if indexBy is set, array_merge will cause renumbering of numeric array
foreach($buckets[$key] as $bucketKey => $bucketValue) {
$value[$bucketKey] = $bucketValue;
}
} else {
$value = array_merge($value, $buckets[$key]);
}
}
}
} else {
......
......@@ -45,6 +45,12 @@ class Order extends ActiveRecord
})->orderBy('item.id');
}
public function getItemsIndexed()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems')->indexBy('id');
}
public function getItemsWithNullFK()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
......
......@@ -48,6 +48,12 @@ class Order extends ActiveRecord
->via('orderItems')->orderBy('id');
}
public function getItemsIndexed()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems')->indexBy('id');
}
public function getItemsWithNullFK()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
......
......@@ -27,6 +27,12 @@ class Order extends ActiveRecord
});
}
public function getItemsIndexed()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems')->indexBy('id');
}
public function getItemsInOrder1()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
......
......@@ -1064,4 +1064,29 @@ trait ActiveRecordTestTrait
$customers = $customerClass::find()->where(['IN', 'id', []])->all();
$this->assertEquals(0, count($customers));
}
public function testFindEagerIndexBy()
{
/* @var $this TestCase|ActiveRecordTestTrait */
/* @var $orderClass \yii\db\ActiveRecordInterface */
$orderClass = $this->getOrderClass();
/* @var $order Order */
$order = $orderClass::find()->with('itemsIndexed')->where(['id' => 1])->one();
$this->assertTrue($order->isRelationPopulated('itemsIndexed'));
$items = $order->itemsIndexed;
$this->assertEquals(2, count($items));
$this->assertTrue(isset($items[1]));
$this->assertTrue(isset($items[2]));
/* @var $order Order */
$order = $orderClass::find()->with('itemsIndexed')->where(['id' => 2])->one();
$this->assertTrue($order->isRelationPopulated('itemsIndexed'));
$items = $order->itemsIndexed;
$this->assertEquals(3, count($items));
$this->assertTrue(isset($items[3]));
$this->assertTrue(isset($items[4]));
$this->assertTrue(isset($items[5]));
}
}
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