Skip to content

Commit

Permalink
Merge pull request #7 from KateAAAA/master
Browse files Browse the repository at this point in the history
Refresh relations on cached models
  • Loading branch information
SonicGD authored Aug 14, 2017
2 parents 69438b9 + 89e16c1 commit 7a477d5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CacheActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace sitkoru\cache\ar;

use yii\db\ActiveQuery;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRecord;

/**
Expand All @@ -15,6 +16,7 @@
class CacheActiveQuery extends ActiveQuery
{

use ActiveQueryTrait;

private $dropConditions = [];
private $disableCache = false;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7a477d5

Please sign in to comment.