mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-22 19:31:02 +08:00
Sphinx Active Relation lazy load fixed.
This commit is contained in:
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -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()
|
||||||
|
Reference in New Issue
Block a user