Skip to content

Commit 8ab9535

Browse files
author
simialbi
committed
added $itemsProperty property for wrapped responses
added caching if enabled
1 parent 0a8011c commit 8ab9535

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Command.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function cache($duration = null, $dependency = null)
6868
$this->queryCacheDependency = $dependency;
6969
return $this;
7070
}
71+
7172
/**
7273
* Disables query cache for this command.
7374
* @return $this the command object itself
@@ -225,7 +226,16 @@ protected function queryInternal($method = 'get')
225226
}
226227
}
227228

228-
return $this->db->$method($this->pathInfo, $this->queryParams);
229+
$result = $this->db->$method($this->pathInfo, $this->queryParams);
230+
if ($this->db->itemsProperty) {
231+
$result = ArrayHelper::getValue($result, $this->db->itemsProperty, []);
232+
}
233+
if (isset($cache, $cacheKey, $info)) {
234+
$cache->set($cacheKey, [$result], $info[1], $info[2]);
235+
Yii::debug('Saved query result in cache', 'simialbi\yii2\rest\Command::query');
236+
}
237+
238+
return $result;
229239
}
230240

231241
/**

src/Connection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class Connection extends Component
9898
* @see enableQueryCache
9999
*/
100100
public $queryCache = 'cache';
101+
/**
102+
* @var string|null the name of the property in the response where the items are wrapped. If not set, there is no
103+
* wrapping property.
104+
*/
105+
public $itemsProperty;
101106

102107
/**
103108
* @var string|Closure authorization config

0 commit comments

Comments
 (0)