From 948029f5836fa31665339f45e9d149be378ad68d Mon Sep 17 00:00:00 2001 From: Arkeins <7311955+Arkeins@users.noreply.github.com> Date: Sun, 4 Dec 2022 17:55:12 +0100 Subject: [PATCH] Fix #19693: Fix db/Command not caching `NULL` result with scalar fetchMode --- framework/CHANGELOG.md | 2 +- framework/db/Command.php | 2 +- tests/framework/db/QueryTest.php | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index c8d1a77e85..eadc5ff691 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,12 +4,12 @@ Yii Framework 2 Change Log 2.0.48 under development ------------------------ +- Bug #19693: Fix db/Command not caching `NULL` result with scalar fetchMode (Arkeins) - Enh #15376: Added cache usage for `yii2\rbac\DbManager::getRolesByUser()` (manchenkoff) - Enh #9740: Usage of DI instead of new keyword in Schemas (manchenkoff) - Enh #19689: Remove empty elements from the `class` array in `yii\helpers\BaseHtml::renderTagAttributes()` to prevent unwanted spaces (MoritzLost) - Chg #19696: Change visibility of `yii\web\View::isPageEnded` to `protected` (lubosdz, samdark) - 2.0.47 November 18, 2022 ------------------------ diff --git a/framework/db/Command.php b/framework/db/Command.php index 6f5d5daede..2b8d77ff60 100644 --- a/framework/db/Command.php +++ b/framework/db/Command.php @@ -1153,7 +1153,7 @@ class Command extends Component $cache = $info[0]; $cacheKey = $this->getCacheKey($method, $fetchMode, ''); $result = $cache->get($cacheKey); - if (is_array($result) && isset($result[0])) { + if (is_array($result) && array_key_exists(0, $result)) { Yii::debug('Query result served from cache', 'yii\db\Command::query'); return $result[0]; } diff --git a/tests/framework/db/QueryTest.php b/tests/framework/db/QueryTest.php index 5dbcc0b0b7..4de2910704 100644 --- a/tests/framework/db/QueryTest.php +++ b/tests/framework/db/QueryTest.php @@ -727,6 +727,11 @@ abstract class QueryTest extends DatabaseTestCase $this->assertEquals('user1', $query->noCache()->where(['id' => 1])->scalar($db)); $this->assertEquals('user11', $query->cache()->where(['id' => 1])->scalar($db)); }, 10); + + $update->bindValues([':id' => 3, ':name' => null])->execute(); + $this->assertEquals(null, $query->cache()->where(['id' => 3])->scalar($db)); + $update->bindValues([':id' => 3, ':name' => 'user3'])->execute(); + $this->assertEquals(null, $query->cache()->where(['id' => 3])->scalar($db), 'Null value should be cached.'); }