magento2: Paging magento 2 not correct when use group in collection
Version : Magento2-0.5 Steps to reproduce
I using code :
$collection = $this->_gridFactory->create()->getCollection();
$tableMessagesReply = $this->_resource->getTableName('magebay_messages_reply');
$collection->getSelect()->joinLeft( array('messages_reply'=>$tableMessagesReply),'main_table.messages_id = messages_reply.messages_id',array('reply_id'=>"MAX(reply_id)"));
$collection->getSelect()->group('main_table.messages_id');
$collection->getSelect()->where('main_table.user_id=?', $this->_customerSession->getId());
$collection->setOrder('reply_id', 'DESC');
Expected result
Actual result
Note
The count total items not correct
Posible fix
I checked and see magento 2 using function getTotalNum to return number total items but if use group by in sql querry the function will return number total of first group by, mean return number total items of table 2 in first group by, this is result not correct , the result correct is return number items of table 1
I noticed that if I change the in method getTotalNum
public function getTotalNum()
{
return $this->getCollection()->getSize();
}
In class
namespace Magento\Theme\Block\Html;
/**
* Html pager block
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
*/
class Pager extends \Magento\Framework\View\Element\Template
Change
return $this->getCollection()->getSize();
to this
return count($this->getCollection());
The result will correct with all case query include group by
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 30 (10 by maintainers)
Commits related to this issue
- Fix for Magento2 bug affecting pagination of product collections with GROUP BY clauses. @see https://github.com/magento/magento2/issues/4767 — committed to Ometria/magento2-extension by chris-pook 5 years ago
- Merge pull request #4767 from magento-thunder/MC-19421 Fixed issues: - MC-19421: Reduce q-ty of Reports Created in /app/*/var/report — committed to magento/magento2 by deleted user 5 years ago
SOLUTION FOR WRONG COUNT WITH GROUPBY
After Spent my lot of time, finally I have find solution for wrong count with groupby Issue and it’s work for me. So, I have share this solution with you all. I hope it’ll help you to.
For solution of wrong count with group you just need to follow 2 step which I listed below.
Open Toolbar.php file from : magento_root/vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php And Find " public function getTotalNum() " Change
return $this->getCollection()->getSize();
to thisreturn $this->getCollection()->count();
Open Collection.php file from : magento_root/vendor/magento/module-catalog/Model/ResourceModel/Product/Collection.php And Add following function at end of the class.
If this solution is working for you then I suggest you don’t direct change to core file just override class and block and add the solution on that.
I Hope this will help you. and incase if this solution is not working for you and you find another solution for that then I’ll request you all the guy’s please share to all.
And I Also thanx to my colleagues Paresh he also help me out to find the solution for this issues.
Thank you, Mayank Zalavadia
Add the following in di.xml
We are overriding 2 core files.
It’s done. Run magento upgrade commands and clean cache.
I believe the issue is in
\Magento\Catalog\Model\ResourceModel\Product\Collection::_getSelectCountSql
as it always sets$countSelect->columns('COUNT(DISTINCT e.entity_id)');
regardless of presence of GROUP BY in the query.I fixed it with the simple plugin as follows:
Hope this helps.
Hello, a similar problem. If i want filter product collection, create plugin for Layer (if use event the result will be the same).
Code LayerPlugin (filter product collection by type_id):
Then i view my product catalog if see 2 product (it’s true), but count 9.
If i look Magento\Catalog\Block\ProductListProduct::_beforeToHtml() $toolbar->setCollection($collection); after this getSize() return 9. then getSize() have $this->_totalRecords = 9, but i call getSize() early i get 2. Example: First:
Result:
Second:
Result:
If use count($collection) i get correct 2.
@mayankzalavadia’s answer is almost correct. In Magento/Catalog/Block/Product/ProductList/Toolbar.php getTotalNum() function must be
public function getTotalNum() { return count($this->getCollection()->getAllIds()); }
and in your Magento\Catalog\Model\ResourceModel\Product\Collection override class the getLastPageNumber() function should be
Use overrides class and do not edit core files and if you have third party extensions that override the Magento\Catalog\Model\ResourceModel\Product\Collection you should override that for this to work.
Hope it helps guys.
Initial issue is fixed https://github.com/magento/magento2/commit/e012a481737ba124281581442f57231bee259aa4. Be free to open new one if that doesn’t help
We have more bugs all pointing to this but there just doesn’t seem to be any real solid fix.
Related: https://github.com/magento/magento2/pull/10246 https://github.com/magento/magento2/issues/7730
All the bug-reports are however closed with no clear proper fix. How is this possible?
@sintsov . There is no problem here. We couldn`t apply any filters to product collection, because from now we using product collection only as wrapper for search fulltext collection
So you need to add your filter here: Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection::_renderFiltersBefore
There you can find SearchCriteriaBuilder, in which you can add your own attribute to filter Note: that your attribute should be present in mysql fulltext index (in solr, elastic search indexes) and attribute should be set as filterable
Also I notice, that type_id is field not attribute