Commit 0adcd06c by Paul Klimov

Fixed lazy load of relations to `yii\mongodb\file\ActiveRecord`

parent cb7131ed
......@@ -4,7 +4,7 @@ Yii Framework 2 mongodb extension Change Log
2.0.2 under development
-----------------------
- no changes in this release.
- Bug #6376: Fixed lazy load of relations to `yii\mongodb\file\ActiveRecord` (klimov-paul)
2.0.1 December 07, 2014
......
......@@ -70,6 +70,38 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
/**
* @inheritdoc
*/
protected function buildCursor($db = null)
{
if ($this->primaryModel !== null) {
// lazy loading
if ($this->via instanceof self) {
// via pivot collection
$viaModels = $this->via->findJunctionRows([$this->primaryModel]);
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/* @var $viaQuery ActiveQuery */
list($viaName, $viaQuery) = $this->via;
if ($viaQuery->multiple) {
$viaModels = $viaQuery->all();
$this->primaryModel->populateRelation($viaName, $viaModels);
} else {
$model = $viaQuery->one();
$this->primaryModel->populateRelation($viaName, $model);
$viaModels = $model === null ? [] : [$model];
}
$this->filterByModels($viaModels);
} else {
$this->filterByModels([$this->primaryModel]);
}
}
return parent::buildCursor($db);
}
/**
* Executes query and returns all results as an array.
* @param \yii\mongodb\Connection $db the Mongo connection used to execute the query.
* If null, the Mongo connection returned by [[modelClass]] will be used.
......
......@@ -2,6 +2,8 @@
namespace yiiunit\data\ar\mongodb;
use yiiunit\data\ar\mongodb\file\CustomerFile;
class Customer extends ActiveRecord
{
public static function collectionName()
......@@ -17,6 +19,7 @@ class Customer extends ActiveRecord
'email',
'address',
'status',
'file_id',
];
}
......@@ -25,6 +28,11 @@ class Customer extends ActiveRecord
return $this->hasMany(CustomerOrder::className(), ['customer_id' => '_id']);
}
public function getFile()
{
return $this->hasOne(CustomerFile::className(), ['_id' => 'file_id']);
}
/**
* @inheritdoc
* @return CustomerQuery
......
<?php
namespace yiiunit\extensions\mongodb\file;
use yiiunit\data\ar\mongodb\Customer;
use yiiunit\data\ar\mongodb\file\CustomerFile;
use yiiunit\extensions\mongodb\MongoDbTestCase;
/**
* @group mongodb
*/
class ActiveRelationTest extends MongoDbTestCase
{
protected function setUp()
{
parent::setUp();
\yiiunit\data\ar\mongodb\ActiveRecord::$db = $this->getConnection();
\yiiunit\data\ar\mongodb\file\ActiveRecord::$db = $this->getConnection();
$this->setUpTestRows();
}
protected function tearDown()
{
$this->dropCollection(Customer::collectionName());
$this->dropCollection(CustomerFile::collectionName());
parent::tearDown();
}
/**
* Sets up test rows.
*/
protected function setUpTestRows()
{
$fileCollection = $this->getConnection()->getFileCollection(CustomerFile::collectionName());
$customers = [];
$files = [];
for ($i = 1; $i <= 5; $i++) {
$file = [
'tag' => 'tag' . $i,
'status' => $i,
];
$content = 'content' . $i;
$file['_id'] = $fileCollection->insertFileContent($content, $file);
$file['content'] = $content;
$files[] = $file;
$customers[] = [
'name' => 'name' . $i,
'email' => 'email' . $i,
'address' => 'address' . $i,
'status' => $i,
'file_id' => $file['_id'],
];
}
$customerCollection = $this->getConnection()->getCollection(Customer::collectionName());
$customers = $customerCollection->batchInsert($customers);
}
// Tests :
public function testFindLazy()
{
/* @var $customer Customer */
$customer = Customer::findOne(['status' => 2]);
$this->assertFalse($customer->isRelationPopulated('file'));
$file = $customer->file;
$this->assertTrue($customer->isRelationPopulated('file'));
$this->assertTrue($file instanceof CustomerFile);
$this->assertEquals((string) $file->_id, (string) $customer->file_id);
$this->assertEquals(1, count($customer->relatedRecords));
}
public function testFindEager()
{
/* @var $customers Customer[] */
$customers = Customer::find()->with('file')->all();
$this->assertEquals(5, count($customers));
$this->assertTrue($customers[0]->isRelationPopulated('file'));
$this->assertTrue($customers[1]->isRelationPopulated('file'));
$this->assertTrue($customers[0]->file instanceof CustomerFile);
$this->assertEquals((string) $customers[0]->file->_id, (string) $customers[0]->file_id);
$this->assertTrue($customers[1]->file instanceof CustomerFile);
$this->assertEquals((string) $customers[1]->file->_id, (string) $customers[1]->file_id);
}
}
\ No newline at end of file
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