我正在使用Magento 1.4版,並且向Sales Order Grid添加額外的網格列(名稱和Skus),返回的數據是正確的, m具有與分頁和總記錄數,我的代碼如下問題:Magento銷售訂單網格添加名稱和Skus列時顯示不正確的記錄數
首先我編輯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類
public function getSelectCountSql()
{
$countSelect = parent::getSelectCountSql();
//added
$countSelect->reset(Zend_Db_Select::HAVING);
//end
$countSelect->resetJoinLeft();
return $countSelect;
}
任何知道如何才能計算的行數這種方法getSelectCountSql()?提前致謝。
詹姆斯解決方案肯定會更好,Meabed重寫getSize會觸發綁定的丟失。我用「group by」子句實現了一個複雜的集合,並且遇到了「count = 1」的錯誤。添加'$ countSelect-> reset(Zend_Db_Select :: GROUP);'getSelectCountSql以乾淨的方式完成了這個訣竅。 – SMASHED 2012-10-23 13:38:49