using group() breaks getSelectCountSql in magento

后端 未结 5 544
无人及你
无人及你 2020-12-08 05:50

When I\'m using

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

or

$collection->groupByAttribute(\'entity_id\')
<         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 06:25

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

提交回复
热议问题