Magento集合GroupBy getSize

Magento集合GroupBy getSize,magento,collections,count,group-by,Magento,Collections,Count,Group By,我有一个收藏: $this->_totalVersions = Mage::getModel('downloads/files') ->getCollection() ->addFieldToFilter('customer_groups', array('like' => '%'.$customergroupId.'%'))

我有一个收藏:

$this->_totalVersions = Mage::getModel('downloads/files')
                                ->getCollection()
                                ->addFieldToFilter('customer_groups', array('like' => '%'.$customergroupId.'%'))
                                ->addFieldToFilter('main_table.is_active', 1)
                                ->getSize()
很好用!但是,当我添加一个
组时,它不起作用

$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()
我不想使用
->count()
count($collection)
因为它可以容纳90000多个项目

有没有一个正确的方法来计算收集的数量

提前感谢,

Martijn

方法“getSize()”是在Varien_Data_集合上实现的,当您在集合中调用“getSelect()”时,它将返回一个“Zend_Db_Select”对象,而此对象不实现getSize方法

因此,您可以使用magento在集合上实现getSize的方式,但在Zend_Db_Select中,您不能仅在Zend_Db_Select中对集合进行分组

Magento这样做:

 $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(*)');
明白吗


Magento重置查询的所有“重”参数,只需使用“COUNT()”进行查询,但如果需要使用group just COUNT(),则会对组进行计数,因此您可以使用DISTINCT模拟组结果。

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

几个小时后我就解决了。。诀窍是在资源集合
XXXXX\u下载\u模型\u Mysql4\u文件\u集合中添加一个过滤器

public function addGroupByNameFilter()
{
    $this->getSelect()->group('main_table.name');
    return $this;
}
应用此筛选器:

$this->_totalItems = Mage::getModel('downloads/files')
        ->getCollection()
        ->addFieldToFilter('customer_groups', array('like' => '%'.$customergroupId.'%'))
        ->addFieldToFilter('main_table.is_active', 1)
        ->addGroupByNameFilter()
        ->getSize()
工作起来很有魅力!这样,我将保留我的
XXXXXXX\u下载\u模型\u Mysql4\u文件\u集合
对象。:)

干杯


马蒂恩

太棒了!添加参数
groupBy($field,$table=null)
使它变得完美