Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Count doesn't work with group by columns. This fix keeps the group by #1404

Merged
merged 7 commits into from
Mar 10, 2021
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,15 @@
"contributions": [
"code"
]
},
{
"login": "dbachmann",
"name": "Daniel Bachmann",
"avatar_url": "https://avatars1.githubusercontent.com/u/1921769?v=4",
"profile": "https://github.com/dbachmann",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7
Expand Down
13 changes: 11 additions & 2 deletions lib/Varien/Data/Collection/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,17 @@ public function getSelectCountSql()
$countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
$countSelect->reset(Zend_Db_Select::COLUMNS);

$countSelect->columns('COUNT(*)');

if (count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) {
$countSelect->reset(Zend_Db_Select::GROUP);
$countSelect->distinct(true);
$group = $this->getSelect()->getPart(Zend_Db_Select::GROUP);
$group = array_map(function($token) {
return $this->getSelect()->getAdapter()->quoteIdentifier($token, true);
}, $group);
$countSelect->columns("COUNT(DISTINCT " . implode(", ", $group) . ")");
colinmollenhour marked this conversation as resolved.
Show resolved Hide resolved
} else {
$countSelect->columns('COUNT(*)');
}
return $countSelect;
}

Expand Down