From 1830dd8f3898409ab92b25ad5577a8a09c5a0cff Mon Sep 17 00:00:00 2001 From: Colin Mollenhour Date: Fri, 26 May 2023 10:27:09 -0400 Subject: [PATCH] Fix regression from getSelectCountSql optimization when collection uses bind parameters in join clauses. Refs #2210 --- lib/Varien/Data/Collection/Db.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/Varien/Data/Collection/Db.php b/lib/Varien/Data/Collection/Db.php index b20a4791d38..a7af8909e98 100644 --- a/lib/Varien/Data/Collection/Db.php +++ b/lib/Varien/Data/Collection/Db.php @@ -240,8 +240,11 @@ public function getSelectCountSql() } else { $countSelect->columns('COUNT(*)'); - // Simple optimization - remove all joins if there are no where clauses using joined tables and all joins are left joins - $leftJoins = array_filter($countSelect->getPart(Zend_Db_Select::FROM), function ($table) { + // Simple optimization - remove all joins if: + // - there are no where clauses using joined tables + // - all joins are left joins + // - there are no join conditions using bind params (for simplicity) + $leftJoins = array_filter($countSelect->getPart(Zend_Db_Select::FROM), function($table) { return ($table['joinType'] == Zend_Db_Select::LEFT_JOIN || $table['joinType'] == Zend_Db_Select::FROM); }); if (count($leftJoins) == count($countSelect->getPart(Zend_Db_Select::FROM))) { @@ -258,7 +261,13 @@ public function getSelectCountSql() return !preg_match($pattern, $clause); }); }); - if (empty($whereUsingJoin)) { + if ($this->_bindParams) { + $bindPattern = '/('.implode('|', array_keys($this->_bindParams)).')/'; + $joinUsingBind = array_filter($leftJoins, function ($table) use ($bindPattern) { + return preg_match($bindPattern, $table['joinCondition']); + }); + } + if (empty($whereUsingJoin) && empty($joinUsingBind)) { $from = array_slice($leftJoins, 0, 1); $countSelect->setPart(Zend_Db_Select::FROM, $from); }