Commit 15c8935d by Klimov Paul

Fixed `yii\mongodb\ActiveRecord` unable to fetch 'hasMany' referred by array of `\MongoId`

parent 85a9887a
......@@ -5,6 +5,7 @@ Yii Framework 2 mongodb extension Change Log
-----------------------
- Bug #5303: Fixed `yii\mongodb\Collection` unable to fetch default database name from DSN with parameters (klimov-paul)
- Bug #5411: Fixed `yii\mongodb\ActiveRecord` unable to fetch 'hasMany' referred by array of `\MongoId` (klimov-paul)
2.0.0-rc September 27, 2014
......
......@@ -252,6 +252,9 @@ trait ActiveRelationTrait
if ($this->multiple && count($link) == 1 && is_array($keys = $primaryModel[reset($link)])) {
$value = [];
foreach ($keys as $key) {
if (!is_scalar($key)) {
$key = serialize($key);
}
if (isset($buckets[$key])) {
if ($this->indexBy !== null) {
// if indexBy is set, array_merge will cause renumbering of numeric array
......
......@@ -15,7 +15,7 @@ class CustomerOrder extends ActiveRecord
'_id',
'number',
'customer_id',
'items',
'item_ids',
];
}
......@@ -23,4 +23,9 @@ class CustomerOrder extends ActiveRecord
{
return $this->hasOne(Customer::className(), ['_id' => 'customer_id']);
}
public function getItems()
{
return $this->hasMany(Item::className(), ['_id' => 'item_ids']);
}
}
<?php
namespace yiiunit\data\ar\mongodb;
class Item extends ActiveRecord
{
public static function collectionName()
{
return 'item';
}
public function attributes()
{
return [
'_id',
'name',
'price',
];
}
}
\ No newline at end of file
......@@ -30,8 +30,6 @@ class ActiveRelationTest extends MongoDbTestCase
*/
protected function setUpTestRows()
{
$customerCollection = $this->getConnection()->getCollection('customer');
$customers = [];
for ($i = 1; $i <= 5; $i++) {
$customers[] = [
......@@ -41,20 +39,39 @@ class ActiveRelationTest extends MongoDbTestCase
'status' => $i,
];
}
$customerCollection->batchInsert($customers);
$customerCollection = $this->getConnection()->getCollection('customer');
$customers = $customerCollection->batchInsert($customers);
$items = [];
for ($i = 1; $i <= 10; $i++) {
$items[] = [
'name' => 'name' . $i,
'price' => $i,
];
}
$itemCollection = $this->getConnection()->getCollection('item');
$items = $itemCollection->batchInsert($items);
$customerOrderCollection = $this->getConnection()->getCollection('customer_order');
$customerOrders = [];
foreach ($customers as $customer) {
foreach ($customers as $i => $customer) {
$customerOrders[] = [
'customer_id' => $customer['_id'],
'number' => $customer['status'],
'item_ids' => [
$items[$i]['_id'],
$items[$i+5]['_id'],
],
];
$customerOrders[] = [
'customer_id' => $customer['_id'],
'number' => $customer['status'] + 100,
'item_ids' => [
$items[$i]['_id'],
$items[$i+5]['_id'],
],
];
}
$customerOrderCollection = $this->getConnection()->getCollection('customer_order');
$customerOrderCollection->batchInsert($customerOrders);
}
......@@ -83,4 +100,15 @@ class ActiveRelationTest extends MongoDbTestCase
$this->assertTrue($orders[1]->customer instanceof Customer);
$this->assertEquals((string) $orders[1]->customer->_id, (string) $orders[1]->customer_id);
}
/**
* @see https://github.com/yiisoft/yii2/issues/5411
*
* @depends testFindEager
*/
public function testFindEagerHasManyByArrayKey()
{
$order = CustomerOrder::find()->where(['number' => 1])->with('items')->one();
$this->assertNotEmpty($order->items);
}
}
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