From 89e16c1b2d6195907f9dc9efadb8567da73c804f Mon Sep 17 00:00:00 2001 From: KateAAAA Date: Mon, 14 Aug 2017 11:22:55 +0500 Subject: [PATCH] Refresh relations on cached models --- CacheActiveQuery.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CacheActiveQuery.php b/CacheActiveQuery.php index 3eef150..110c425 100644 --- a/CacheActiveQuery.php +++ b/CacheActiveQuery.php @@ -3,6 +3,7 @@ namespace sitkoru\cache\ar; use yii\db\ActiveQuery; +use yii\db\ActiveQueryTrait; use yii\db\ActiveRecord; /** @@ -15,6 +16,7 @@ class CacheActiveQuery extends ActiveQuery { + use ActiveQueryTrait; private $dropConditions = []; private $disableCache = false; @@ -45,6 +47,20 @@ public function all($db = null) if ($fromCache) { $resultFromCache = []; + if($this->with) + { + $primaryModel = reset($fromCache); + $relations = $this->normalizeRelations($primaryModel, $this->with); + /* @var $relation ActiveQuery */ + foreach ($relations as $name => $relation) + { + if ($relation->asArray === null) { + // inherit asArray from primary query + $relation->asArray($this->asArray); + } + $relation->populateRelation($name, $fromCache); + } + } foreach ($fromCache as $i => $model) { $index = $i; if ($model instanceof ActiveRecord) {