From 1546e26c514110d5bf25bd65917fbf61ab275f26 Mon Sep 17 00:00:00 2001 From: SunwelLight Date: Thu, 31 May 2018 04:51:27 +0600 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D1=8D=D1=88=D0=B0=20=D0=BA=20=D1=81?= =?UTF-8?q?=D0=B2=D1=8F=D0=B7=D1=8F=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit к свойству relations добавлен ключ cacheDuration, если в нём указано значение то запрос будет кэшироваться на это время --- src/LinkerBehavior.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/LinkerBehavior.php b/src/LinkerBehavior.php index 48795fd..59d0ac1 100644 --- a/src/LinkerBehavior.php +++ b/src/LinkerBehavior.php @@ -34,6 +34,7 @@ class LinkerBehavior extends Behavior implements LinkerBehaviorInterface * ``` * 'relations' => [ * 'reviews', + * 'cacheDuration' => 3600, * 'updater' => [ * 'fallbackValue' => 17, * ] @@ -326,6 +327,8 @@ public function canSetProperty($name, $checkVars = true) : parent::canSetProperty($name, $checkVars = true); } + private $foreignModel; + private $relation; /** * {@inheritdoc} * @@ -338,17 +341,29 @@ public function __get($name) $fieldParams = $this->getFieldParams($name); $attributeName = $fieldParams['attribute']; $relationName = $this->getRelationName($attributeName); + $cacheDuration = intval($this->relations[$attributeName]['cacheDuration'] ?? 0); if ($this->hasDirtyValueOfAttribute($attributeName)) { $value = $this->getDirtyValueOfAttribute($attributeName); } else { /** @var ActiveRecord $owner */ $owner = $this->owner; + $relation = $owner->getRelation($relationName); /** @var ActiveRecord $foreignModel */ $foreignModel = Yii::createObject($relation->modelClass); - $value = $relation->select($foreignModel->getPrimaryKey())->column(); + + $this->foreignModel = $foreignModel; + $this->relation = $relation; + + if($cacheDuration) { + $value = $this->foreignModel::getDb()->cache(function ($db) { + return $this->relation->select($this->foreignModel->getPrimaryKey())->column(); + }, $cacheDuration); + } else { + $value = $relation->select($foreignModel->getPrimaryKey())->column(); + } } if (empty($fieldParams['get'])) {