Magento Collection GroupBy getSize
Magento Collection GroupBy getSize
我有一个 collection:
$this->_totalVersions = Mage::getModel('downloads/files')
->getCollection()
->addFieldToFilter('customer_groups', array('like' => '%'.$customergroupId.'%'))
->addFieldToFilter('main_table.is_active', 1)
->getSize()
完美运行!但是当我添加 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()
我不想使用 ->count()
或 count($collection)
因为它拥有 90.000 多件物品。
有没有正确的方法来计算collection?
提前致谢,
马丁
方法 "getSize()" 在 Varien_Data_Collection 上实现,当您在集合中调用 "getSelect()" 时,它 return 是一个 "Zend_Db_Select" 对象,而这个对象不是t 实现 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 重置查询的所有 "heavy" 参数,并且只需使用 "COUNT()" 进行查询,但如果您需要使用组,则只需 COUNT() 将计算您的组,因此您可以使用 DISTINCT 模拟组结果。
首先,感谢 Guerra 的回复。你给我指明了正确的方向。
我几个小时后就解决了..诀窍是在资源集合中添加一个过滤器XXXXX_Downloads_Model_Mysql4_Files_Collection
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_Downloads_Model_Mysql4_Files_Collection
对象。 :)
干杯,
马丁
我有一个 collection:
$this->_totalVersions = Mage::getModel('downloads/files')
->getCollection()
->addFieldToFilter('customer_groups', array('like' => '%'.$customergroupId.'%'))
->addFieldToFilter('main_table.is_active', 1)
->getSize()
完美运行!但是当我添加 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()
我不想使用 ->count()
或 count($collection)
因为它拥有 90.000 多件物品。
有没有正确的方法来计算collection?
提前致谢,
马丁
方法 "getSize()" 在 Varien_Data_Collection 上实现,当您在集合中调用 "getSelect()" 时,它 return 是一个 "Zend_Db_Select" 对象,而这个对象不是t 实现 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 重置查询的所有 "heavy" 参数,并且只需使用 "COUNT()" 进行查询,但如果您需要使用组,则只需 COUNT() 将计算您的组,因此您可以使用 DISTINCT 模拟组结果。
首先,感谢 Guerra 的回复。你给我指明了正确的方向。
我几个小时后就解决了..诀窍是在资源集合中添加一个过滤器XXXXX_Downloads_Model_Mysql4_Files_Collection
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_Downloads_Model_Mysql4_Files_Collection
对象。 :)
干杯,
马丁