Magento Collection Group由getSize提供

Mar*_*oof 0 collections group-by count magento

我有一个集合:

$this->_totalVersions = Mage::getModel('downloads/files')
                                ->getCollection()
                                ->addFieldToFilter('customer_groups', array('like' => '%'.$customergroupId.'%'))
                                ->addFieldToFilter('main_table.is_active', 1)
                                ->getSize()
Run Code Online (Sandbox Code Playgroud)

完美的工作!但是当我添加group它时它不起作用.

$this->_totalVersions = Mage::getModel('downloads/files')
                                ->getCollection()
                                ->addFieldToFilter('customer_groups', array('like' => '%'.$customergroupId.'%'))
                                ->addFieldToFilter('main_table.is_active', 1)
                                ->getSelect()->group(array('main_table.name'))
                                ->getSize()
Run Code Online (Sandbox Code Playgroud)

我不想使用->count()count($collection) 因为它拥有90.000+项.

有没有正确的方法来计算收藏?

提前致谢,

马亭

Mar*_*oof 6

首先,感谢Guerra的回复.你指出我正确的方向.

我刚刚解决了几个小时..诀窍是在资源集合中添加一个过滤器 XXXXX_Downloads_Model_Mysql4_Files_Collection

public function addGroupByNameFilter()
{
    $this->getSelect()->group('main_table.name');
    return $this;
}
Run Code Online (Sandbox Code Playgroud)

应用此过滤器:

$this->_totalItems = Mage::getModel('downloads/files')
        ->getCollection()
        ->addFieldToFilter('customer_groups', array('like' => '%'.$customergroupId.'%'))
        ->addFieldToFilter('main_table.is_active', 1)
        ->addGroupByNameFilter()
        ->getSize()
Run Code Online (Sandbox Code Playgroud)

奇迹般有效!这样我就会保留我的XXXXXXX_Downloads_Model_Mysql4_Files_Collection目标.:)

干杯,

马亭