From 8500033bb8d7e385e917d30ee336d8bd049a56e3 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 9 Jul 2024 12:46:26 -0400 Subject: [PATCH] Revert "Remove cache \yii\data\ActiveDataProvider::prepareTotalCount() (#20206)" This reverts commit 140570d1860a5734f9e47de75bf1197853a3380a. --- framework/data/ActiveDataProvider.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/framework/data/ActiveDataProvider.php b/framework/data/ActiveDataProvider.php index 1a098cd6474..357b1eb74ef 100644 --- a/framework/data/ActiveDataProvider.php +++ b/framework/data/ActiveDataProvider.php @@ -151,6 +151,8 @@ protected function prepareKeys($models) return array_keys($models); } + private $_totalCount = []; + /** * {@inheritdoc} */ @@ -159,8 +161,13 @@ protected function prepareTotalCount() if (!$this->query instanceof QueryInterface) { throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.'); } - $query = clone $this->query; - return (int) $query->limit(-1)->offset(-1)->orderBy([])->count('*', $this->db); + $query = (clone $this->query)->limit(-1)->offset(-1)->orderBy([]); + $key = md5((string)$query); + + if (!array_key_exists($key, $this->_totalCount)) { + $this->_totalCount[$key] = (int)$query->count('*', $this->db); + } + return $this->_totalCount[$key]; } /**