Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using group() breaks getSelectCountSql in magento

Tags:

magento

When I'm using

$collection->getSelect()->group('entity_id')

or

$collection->groupByAttribute('entity_id')

It breaks getSelectCountSql and I'm getting 1 record and 1 page. Magento does

$countSelect->columns('COUNT(DISTINCT e.entity_id)');

Is there a way to fix it?

I run into it,While overriding _prepareCollection of Mage_Adminhtml_Block_Catalog_Product_Grid

Thanks

like image 967
pablo Avatar asked Aug 14 '10 22:08

pablo


Video Answer


1 Answers

I updated the lib/Varien/Data/Collection/Db.php file to allow this as I had to have it work. You'll have to keep track of this for upgrades but it works.

public function getSelectCountSql()
{   
    $this->_renderFilters();
    $countSelect = clone $this->getSelect();
    $countSelect->reset(Zend_Db_Select::ORDER);
    $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
    $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
    $countSelect->reset(Zend_Db_Select::COLUMNS);

    // Count doesn't work with group by columns keep the group by 
    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;
}
like image 169
Eric Avatar answered Sep 23 '22 11:09

Eric