我正在使用 Magento 1.4 版,我向销售订单网格添加了额外的网格列(名称和 sku),返回的数据是正确的,但我在分页和记录总数方面遇到问题,我的代码如下:
首先我编辑了Mage_Adminhtml_Block_Sales_Order_Grid
:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ", ")'),
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ", ")'),
)
);
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
然后我重写此方法,以便在按名称或 sku 过滤时返回正确的结果
protected function _addColumnFilterToCollection($column)
{
if($this->getCollection() && $column->getFilter()->getValue())
{
if($column->getId() == 'skus'){
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ", ")'),
)
)->getSelect()
->having('find_in_set(?, skus)', $column->getFilter()->getValue());
return $this;
}
if($column->getId() == 'names'){
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ", ")'),
)
)->getSelect()
->having('find_in_set(?, names)', $column->getFilter()->getValue());
return $this;
}
}
return parent::_addColumnFilterToCollection($column);
}
然后我在Mage_Sales_Model_Mysql4_Order_Collection
类中编辑了这个方法getSelectCountSql()
:
public function getSelectCountSql()
{
$countSelect = parent::getSelectCountSql();
//added
$countSelect->reset(Zend_Db_Select::HAVING);
//end
$countSelect->resetJoinLeft();
return $countSelect;
}
如何计算行数?
最佳答案
也许有点晚了,但在你的代码中尝试使用 GROUP 代替 HAVING:
$countSelect->reset(Zend_Db_Select::GROUP);
因为您正在使用此声明:
$collection->getSelect()->group('entity_id');
关于添加名称和 Skus 列时,Magento 销售订单网格显示不正确的记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8764543/