Declaration of overridden method should be compatible with parent class
-
01-03-2021 - |
سؤال
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?
المحلول
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;
}
}
نصائح أخرى
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
لا تنتمي إلى magento.stackexchange