Skip to content

Commit e001493

Browse files
committed
#26986 Fix invalid value returned for "records found" section in Admin Panel
1 parent 6771122 commit e001493

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,9 +1013,9 @@ public function getMaxAttributeValue($attribute)
10131013
$tableAlias = $attributeCode . '_max_value';
10141014
$fieldAlias = 'max_' . $attributeCode;
10151015
$condition = 'e.entity_id = ' . $tableAlias . '.entity_id AND ' . $this->_getConditionSql(
1016-
$tableAlias . '.attribute_id',
1017-
$attribute->getId()
1018-
);
1016+
$tableAlias . '.attribute_id',
1017+
$attribute->getId()
1018+
);
10191019

10201020
$select->join(
10211021
[$tableAlias => $attribute->getBackend()->getTable()],
@@ -1048,9 +1048,9 @@ public function getAttributeValueCountByRange($attribute, $range)
10481048
$tableAlias = $attributeCode . '_range_count_value';
10491049

10501050
$condition = 'e.entity_id = ' . $tableAlias . '.entity_id AND ' . $this->_getConditionSql(
1051-
$tableAlias . '.attribute_id',
1052-
$attribute->getId()
1053-
);
1051+
$tableAlias . '.attribute_id',
1052+
$attribute->getId()
1053+
);
10541054

10551055
$select->reset(\Magento\Framework\DB\Select::GROUP);
10561056
$select->join(
@@ -1088,9 +1088,9 @@ public function getAttributeValueCount($attribute)
10881088

10891089
$select->reset(\Magento\Framework\DB\Select::GROUP);
10901090
$condition = 'e.entity_id=' . $tableAlias . '.entity_id AND ' . $this->_getConditionSql(
1091-
$tableAlias . '.attribute_id',
1092-
$attribute->getId()
1093-
);
1091+
$tableAlias . '.attribute_id',
1092+
$attribute->getId()
1093+
);
10941094

10951095
$select->join(
10961096
[$tableAlias => $attribute->getBackend()->getTable()],
@@ -1180,9 +1180,27 @@ protected function _getSelectCountSql(?Select $select = null, $resetLeftJoins =
11801180
if ($resetLeftJoins) {
11811181
$countSelect->resetJoinLeft();
11821182
}
1183+
1184+
$this->removeEntityIdentifierFromGroupBy($countSelect);
1185+
11831186
return $countSelect;
11841187
}
11851188

1189+
/**
1190+
* Using `entity_id` for `GROUP BY` causes COUNT() return {n} rows of value = 1 instead of 1 row of value {n}
1191+
*
1192+
* @param Select $select
1193+
* @throws \Zend_Db_Select_Exception
1194+
*/
1195+
private function removeEntityIdentifierFromGroupBy(Select $select): void
1196+
{
1197+
$groupBy = array_filter($select->getPart(Select::GROUP), function ($field) {
1198+
return false === strpos($field, $this->getIdFieldName());
1199+
});
1200+
1201+
$select->setPart(Select::GROUP, $groupBy);
1202+
}
1203+
11861204
/**
11871205
* Prepare statistics data
11881206
*
@@ -1766,28 +1784,28 @@ public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
17661784
protected function _prepareProductLimitationFilters()
17671785
{
17681786
if (isset(
1769-
$this->_productLimitationFilters['visibility']
1770-
) && !isset(
1771-
$this->_productLimitationFilters['store_id']
1772-
)
1787+
$this->_productLimitationFilters['visibility']
1788+
) && !isset(
1789+
$this->_productLimitationFilters['store_id']
1790+
)
17731791
) {
17741792
$this->_productLimitationFilters['store_id'] = $this->getStoreId();
17751793
}
17761794
if (isset(
1777-
$this->_productLimitationFilters['category_id']
1778-
) && !isset(
1779-
$this->_productLimitationFilters['store_id']
1780-
)
1795+
$this->_productLimitationFilters['category_id']
1796+
) && !isset(
1797+
$this->_productLimitationFilters['store_id']
1798+
)
17811799
) {
17821800
$this->_productLimitationFilters['store_id'] = $this->getStoreId();
17831801
}
17841802
if (isset(
1785-
$this->_productLimitationFilters['store_id']
1786-
) && isset(
1787-
$this->_productLimitationFilters['visibility']
1788-
) && !isset(
1789-
$this->_productLimitationFilters['category_id']
1790-
)
1803+
$this->_productLimitationFilters['store_id']
1804+
) && isset(
1805+
$this->_productLimitationFilters['visibility']
1806+
) && !isset(
1807+
$this->_productLimitationFilters['category_id']
1808+
)
17911809
) {
17921810
$this->_productLimitationFilters['category_id'] = $this->_storeManager->getStore(
17931811
$this->_productLimitationFilters['store_id']
@@ -1820,12 +1838,12 @@ protected function _productLimitationJoinWebsite()
18201838
'int'
18211839
);
18221840
} elseif (isset(
1823-
$filters['store_id']
1824-
) && (!isset(
1825-
$filters['visibility']
1826-
) && !isset(
1827-
$filters['category_id']
1828-
)) && !$this->isEnabledFlat()
1841+
$filters['store_id']
1842+
) && (!isset(
1843+
$filters['visibility']
1844+
) && !isset(
1845+
$filters['category_id']
1846+
)) && !$this->isEnabledFlat()
18291847
) {
18301848
$joinWebsite = true;
18311849
$websiteId = $this->_storeManager->getStore($filters['store_id'])->getWebsiteId();
@@ -1901,9 +1919,9 @@ protected function _productLimitationJoinPrice()
19011919
/**
19021920
* Join Product Price Table with left-join possibility
19031921
*
1904-
* @see \Magento\Catalog\Model\ResourceModel\Product\Collection::_productLimitationJoinPrice()
19051922
* @param bool $joinLeft
19061923
* @return $this
1924+
* @see \Magento\Catalog\Model\ResourceModel\Product\Collection::_productLimitationJoinPrice()
19071925
*/
19081926
protected function _productLimitationPrice($joinLeft = false)
19091927
{
@@ -2334,8 +2352,8 @@ public function addPriceDataFieldFilter($comparisonFormat, $fields)
23342352
* @return $this
23352353
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
23362354
* @SuppressWarnings(PHPMD.NPathComplexity)
2337-
* @since 101.0.1
23382355
* @throws \Magento\Framework\Exception\LocalizedException
2356+
* @since 101.0.1
23392357
*/
23402358
public function addMediaGalleryData()
23412359
{
@@ -2360,7 +2378,7 @@ public function addMediaGalleryData()
23602378
'entity.' . $linkField . ' IN (?)',
23612379
array_map(
23622380
function ($item) use ($linkField) {
2363-
return (int) $item->getOrigData($linkField);
2381+
return (int)$item->getOrigData($linkField);
23642382
},
23652383
$items
23662384
)
@@ -2411,9 +2429,9 @@ private function getGalleryReadHandler()
24112429
/**
24122430
* Retrieve Media gallery resource.
24132431
*
2432+
* @return \Magento\Catalog\Model\ResourceModel\Product\Gallery
24142433
* @deprecated 101.0.1
24152434
*
2416-
* @return \Magento\Catalog\Model\ResourceModel\Product\Gallery
24172435
*/
24182436
private function getMediaGalleryResource()
24192437
{

0 commit comments

Comments
 (0)