using group() breaks getSelectCountSql in magento

2019-01-16 22:09发布

问题:

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

回答1:

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


回答2:

in some cases, the method from Eric doesn't work.

It's better to rewrite the getSize() method to

    public function getSize()
{
    if (is_null($this->_totalRecords)) {
        $sql = $this->getSelectCountSql();

        $result = $this->getConnection()->fetchAll($sql, $this->_bindParams);;

        foreach ($result as $row) {
            $this->_totalRecords += reset($row);
        }

    }
    return intval($this->_totalRecords);
}


回答3:

I have solved this using the function below:

public function getSize()
{
    return sizeof( $this->getAllIds());
}

This helped me in issue of getSize() returning 1 count of product collection in Magento CE 1.5.

I have overwritten the Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection file and place above function.



回答4:

Very nice article Here are few changes I made in order to make it work for me

Changes suggested in Db.php are not required for resolving group by in catalog collection. I made the similar change in Catalog->model->Ressource->EAV->Mysql4->product->collection.php Here is the code I added to getSelectCountSql()

if(count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) { $countSelect->reset(Zend_Db_Select::GROUP); }

After this things get resolved but a new issue comes in .. In Layered navigation quantities for all the filters is 1.



回答5:

I made it without touching Core files by overriding the getSize() method of my collection.

public function getSize()
{
    if (count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) {

        // Create a new collection from ids because we need a fresh select
        $ids = $this->getAllIds();
        $new_coll = Mage::getModel('module_key/model')->getCollection()
                ->addFieldToFilter('id', array('in' => $ids));

        // return the collection size
        return $new_coll->getSize();
    }

    return parent::getSize();
}

Tell me if that works for you..

Bouni



标签: magento