From 32c7c4161bcf7b5dc1324ba8f4145bcc643891a8 Mon Sep 17 00:00:00 2001 From: drlibra <13391053+drlibra@users.noreply.github.com> Date: Fri, 27 Jul 2018 21:39:16 +0500 Subject: [PATCH] Fixes #16192: `yii\db\Command::logQuery()` is now protected, extracted `getCacheKey()` from `queryInternal()` --- framework/CHANGELOG.md | 2 +- framework/db/Command.php | 32 ++++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 3a2d155e53..11ceede67d 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -37,7 +37,7 @@ Yii Framework 2 Change Log - Bug #16292: Fixed misconfigured CORS filter exception throwing. Now it throws `InvalidConfigException` in Debug mode (khvalov) - Bug #16301: Fixed `yii\web\User::setIdentity()` to clear access check cache while setting identity object to `null` (Izumi-kun) - Bug #16322: Fixed strings were not were not compared using timing attack resistant approach while CSRF token validation (samdark, Felix Wiedemann) -- Chg #16192: `yii\db\Command::logQuery()` is now protected (drlibra) +- Chg #16192: `yii\db\Command::logQuery()` is now protected, extracted `getCacheKey()` from `queryInternal()` (drlibra) - Bug #16377: Fixed `yii\base\Event:off()` undefined index error when event handler does not match (razvanphp) 2.0.15.1 March 21, 2018 diff --git a/framework/db/Command.php b/framework/db/Command.php index 984204fecb..d78b2cce89 100644 --- a/framework/db/Command.php +++ b/framework/db/Command.php @@ -1140,14 +1140,8 @@ class Command extends Component if (is_array($info)) { /* @var $cache \yii\caching\CacheInterface */ $cache = $info[0]; - $cacheKey = [ - __CLASS__, - $method, - $fetchMode, - $this->db->dsn, - $this->db->username, - $rawSql ?: $rawSql = $this->getRawSql(), - ]; + $rawSql = $rawSql ?: $this->getRawSql(); + $cacheKey = $this->getCacheKey($method, $fetchMode, $rawSql); $result = $cache->get($cacheKey); if (is_array($result) && isset($result[0])) { Yii::debug('Query result served from cache', 'yii\db\Command::query'); @@ -1187,6 +1181,28 @@ class Command extends Component return $result; } + /** + * Returns the cache key for the query. + * + * @param string $method method of PDOStatement to be called + * @param int $fetchMode the result fetch mode. Please refer to [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php) + * for valid fetch modes. + * @param string $rawSql the raw SQL with parameter values inserted into the corresponding placeholders + * @return array the cache key + * @since 2.0.16 + */ + protected function getCacheKey($method, $fetchMode, $rawSql) + { + return [ + __CLASS__, + $method, + $fetchMode, + $this->db->dsn, + $this->db->username, + $rawSql, + ]; + } + /** * Marks a specified table schema to be refreshed after command execution. * @param string $name name of the table, which schema should be refreshed.