using group() breaks getSelectCountSql in magento

后端 未结 5 429
无人及你
无人及你 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;
    }
    
    0 讨论(0)
  • 2020-12-08 06:29

    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.

    0 讨论(0)
  • 2020-12-08 06:30

    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.

    0 讨论(0)
  • 2020-12-08 06:34

    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);
    }
    
    0 讨论(0)
  • 2020-12-08 06:38

    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

    0 讨论(0)
提交回复
热议问题