Skip to content

Commit 26b2101

Browse files
author
Mastiuhin Olexandr
committed
MAGETWO-94306: Fixing issue with getSize function not recalculating after adding filters
1 parent 3be6811 commit 26b2101

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ public function addAttributeToFilter($attribute, $condition = null, $joinType =
379379

380380
if (!empty($conditionSql)) {
381381
$this->getSelect()->where($conditionSql, null, \Magento\Framework\DB\Select::TYPE_CONDITION);
382+
$this->invalidateSize();
382383
} else {
383384
throw new \Magento\Framework\Exception\LocalizedException(
384385
__('Invalid attribute identifier for filter (%1)', get_class($attribute))
@@ -1699,4 +1700,16 @@ public function removeAllFieldsFromSelect()
16991700
{
17001701
return $this->removeAttributeToSelect();
17011702
}
1703+
1704+
/**
1705+
* Invalidates "Total Records Count".
1706+
* Invalidates saved "Total Records Count" attribute with last counting,
1707+
* so a next calling of method getSize() will query new total records count.
1708+
*
1709+
* @return void
1710+
*/
1711+
private function invalidateSize(): void
1712+
{
1713+
$this->_totalRecords = null;
1714+
}
17021715
}

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Product/CollectionTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,15 @@ public function testJoinTable()
187187

188188
self::assertContains($expected, str_replace(PHP_EOL, '', $sql));
189189
}
190+
191+
/**
192+
* @magentoDataFixture Magento/Catalog/Model/ResourceModel/_files/few_simple_products.php
193+
* @magentoDbIsolation enabled
194+
*/
195+
public function testAddAttributeToFilterAffectsGetSize(): void
196+
{
197+
$this->assertEquals(10, $this->collection->getSize());
198+
$this->collection->addAttributeToFilter('sku', 'Product1');
199+
$this->assertEquals(1, $this->collection->getSize());
200+
}
190201
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
use Magento\Catalog\Model\ProductFactory;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
12+
/** @var Magento\Framework\ObjectManagerInterface $objcetManager */
13+
$objectManager = Bootstrap::getObjectManager();
14+
15+
/** @var ProductFactory $productFactory */
16+
$productFactory = $objectManager->create(ProductFactory::class);
17+
18+
/** @var ProductRepositoryInterface $productRepository */
19+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
20+
21+
// Create 10 products (with change this variable, don't forget to change the same in rollback)
22+
$productsAmount = 10;
23+
24+
for ($i = 1; $i <= $productsAmount; $i++) {
25+
$productArray = [
26+
'data' => [
27+
'name' => "Product{$i}",
28+
'sku' => "Product{$i}",
29+
'price' => 100,
30+
'attribute_set_id' => 4,
31+
'website_ids' => [1]
32+
]
33+
];
34+
35+
$productRepository->save($productFactory->create($productArray));
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
12+
/** @var ProductRepositoryInterface $productRepository */
13+
$productRepository = Bootstrap::getObjectManager()
14+
->get(ProductRepositoryInterface::class);
15+
16+
/**
17+
* Delete 10 products
18+
*/
19+
$productsAmount = 10;
20+
21+
try {
22+
for ($i = 1; $i <= $productsAmount; $i++) {
23+
/** @var \Magento\Catalog\Model\Product $product */
24+
$product = $productRepository->get("Product{$i}", false, null, true);
25+
$productRepository->delete($product);
26+
}
27+
} catch (NoSuchEntityException $e) {
28+
}

lib/internal/Magento/Framework/Data/Collection/AbstractDb.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function getSize()
219219
$sql = $this->getSelectCountSql();
220220
$this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
221221
}
222-
return intval($this->_totalRecords);
222+
return (int)$this->_totalRecords;
223223
}
224224

225225
/**

0 commit comments

Comments
 (0)