1

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?

1
  • which magento version are you using? Commented May 16, 2019 at 11:17

2 Answers 2

1

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;
}
}
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.