Browse Source

Merge branch 'master' of github.com:yiisoft/yii2 into authclient

tags/2.0.0-beta
Paul Klimov 11 years ago
parent
commit
91000e8d8c
  1. 31
      extensions/yii/mongodb/ActiveRelation.php
  2. 31
      extensions/yii/sphinx/ActiveRelation.php
  3. 9
      tests/unit/extensions/mongodb/ActiveRelationTest.php
  4. 1
      tests/unit/extensions/sphinx/ActiveRelationTest.php

31
extensions/yii/mongodb/ActiveRelation.php

@ -19,4 +19,35 @@ use yii\db\ActiveRelationTrait;
class ActiveRelation extends ActiveQuery implements ActiveRelationInterface class ActiveRelation extends ActiveQuery implements ActiveRelationInterface
{ {
use ActiveRelationTrait; use ActiveRelationTrait;
/**
* @inheritdoc
*/
protected function buildCursor($db = null)
{
if ($this->primaryModel !== null) {
// lazy loading
if ($this->via instanceof self) {
// via pivot collection
$viaModels = $this->via->findPivotRows([$this->primaryModel]);
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/** @var ActiveRelation $viaQuery */
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);
}
} }

31
extensions/yii/sphinx/ActiveRelation.php

@ -19,4 +19,35 @@ use yii\db\ActiveRelationTrait;
class ActiveRelation extends ActiveQuery implements ActiveRelationInterface class ActiveRelation extends ActiveQuery implements ActiveRelationInterface
{ {
use ActiveRelationTrait; use ActiveRelationTrait;
/**
* @inheritdoc
*/
public function createCommand($db = null)
{
if ($this->primaryModel !== null) {
// lazy loading
if ($this->via instanceof self) {
// via pivot index
$viaModels = $this->via->findPivotRows([$this->primaryModel]);
$this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/** @var ActiveRelation $viaQuery */
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::createCommand($db);
}
} }

9
tests/unit/extensions/mongodb/ActiveRelationTest.php

@ -52,7 +52,7 @@ class ActiveRelationTest extends MongoDbTestCase
]; ];
$customerOrders[] = [ $customerOrders[] = [
'customer_id' => $customer['_id'], 'customer_id' => $customer['_id'],
'number' => $customer['status'] + 1, 'number' => $customer['status'] + 100,
]; ];
} }
$customerOrderCollection->batchInsert($customerOrders); $customerOrderCollection->batchInsert($customerOrders);
@ -65,9 +65,10 @@ class ActiveRelationTest extends MongoDbTestCase
/** @var CustomerOrder $order */ /** @var CustomerOrder $order */
$order = CustomerOrder::find(['number' => 2]); $order = CustomerOrder::find(['number' => 2]);
$this->assertFalse($order->isRelationPopulated('customer')); $this->assertFalse($order->isRelationPopulated('customer'));
$index = $order->customer; $customer = $order->customer;
$this->assertTrue($order->isRelationPopulated('customer')); $this->assertTrue($order->isRelationPopulated('customer'));
$this->assertTrue($index instanceof Customer); $this->assertTrue($customer instanceof Customer);
$this->assertEquals((string)$customer->_id, (string)$order->customer_id);
$this->assertEquals(1, count($order->populatedRelations)); $this->assertEquals(1, count($order->populatedRelations));
} }
@ -78,6 +79,8 @@ class ActiveRelationTest extends MongoDbTestCase
$this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('customer'));
$this->assertTrue($orders[1]->isRelationPopulated('customer')); $this->assertTrue($orders[1]->isRelationPopulated('customer'));
$this->assertTrue($orders[0]->customer instanceof Customer); $this->assertTrue($orders[0]->customer instanceof Customer);
$this->assertEquals((string)$orders[0]->customer->_id, (string)$orders[0]->customer_id);
$this->assertTrue($orders[1]->customer instanceof Customer); $this->assertTrue($orders[1]->customer instanceof Customer);
$this->assertEquals((string)$orders[1]->customer->_id, (string)$orders[1]->customer_id);
} }
} }

1
tests/unit/extensions/sphinx/ActiveRelationTest.php

@ -30,6 +30,7 @@ class ActiveRelationTest extends SphinxTestCase
$this->assertTrue($article->isRelationPopulated('index')); $this->assertTrue($article->isRelationPopulated('index'));
$this->assertTrue($index instanceof ArticleIndex); $this->assertTrue($index instanceof ArticleIndex);
$this->assertEquals(1, count($article->populatedRelations)); $this->assertEquals(1, count($article->populatedRelations));
$this->assertEquals($article->id, $index->id);
} }
public function testFindEager() public function testFindEager()

Loading…
Cancel
Save