Question

This is my custom class:

class Collection extends \Magento\CatalogSearch\Model\ResourceModel\Search\Collection
{

public function _getSelectCountSql($select = null, $resetLeftJoins = true)
{

    $this->_renderFilters();
    $countSelect = is_null($select) ? $this->_getClearSelect() : $this->_buildClearSelect($select);
    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);
        $countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")");
    } else {
        $countSelect->columns('COUNT(*)');
    }
    return $countSelect;
}
}

Why Im getting Declaration of overridden method should be compatible with parent class error.

How to fix it?

Was it helpful?

Solution

Your code like this.

use Magento\Framework\DB\Select;

class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
{

public function _getSelectCountSql(?Select $select = null, $resetLeftJoins = true)
{

    $this->_renderFilters();
    $countSelect = is_null($select) ? $this->_getClearSelect() : $this->_buildClearSelect($select);
    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);
        $countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")");
    } else {
        $countSelect->columns('COUNT(*)');
    }
    return $countSelect;
}
}

OTHER TIPS

You have to extend

Magento\Catalog\Model\ResourceModel\Product\Collection class

instead of \Magento\CatalogSearch\Model\ResourceModel\Search\Collection class

because _getSelectCountSql exists in Product\Collection

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top