2012-01-06 57 views
5

我正在使用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()?提前致謝。

回答

0

我有這個問題,我有它通過集合中實現自定義的getSize()函數工作我使用

public function getSize() 
{ 
    $select = clone $this->getSelect(); 
    $select->reset(); 
    $select = $this->getConnection()->fetchOne('SELECT COUNT(*) FROM Table GROUP BY FIELD'); // or you can use select count(distinct field) from table 
    return $select; 
} 

,並實現電網存儲我有越權

protected function _setCollectionOrder($column) 
    { 
     $collection = $this->getCollection(); 
     if ($collection) { 
      $columnIndex = $column->getFilterIndex() ? 
       $column->getFilterIndex() : $column->getIndex(); 
      $collection->getSelect()->order(array($columnIndex.' '.$column->getDir())); 
     } 
     return $this; 
    } 

和設置列的filter_index至

in _prepareColumns() function 
    'filter_index' => 'SUM(tablename.field)' 

並且您可以在f上使用回調函數對於列

+0

詹姆斯解決方案肯定會更好,Meabed重寫getSize會觸發綁定的丟失。我用「group by」子句實現了一個複雜的集合,並且遇到了「count = 1」的錯誤。添加'$ countSelect-> reset(Zend_Db_Select :: GROUP);'getSelectCountSql以乾淨的方式完成了這個訣竅。 – SMASHED 2012-10-23 13:38:49

4

ilters也許它有點晚,但在你的代碼嘗試使用GROUP insted的具有:

$countSelect->reset(Zend_Db_Select::GROUP); 

由於您使用此statemen:

$collection->getSelect()->group('entity_id'); 
+0

但它期待在Mage_Sales_Model_Resource_Order_Collection而不是Mage_Sales_Model_Mysql4_Order_Collection中進行更改,以使更改顯示。你能解釋一下嗎? – RIK 2013-08-12 04:07:48

2
$collection->getSelect()->join(array(
      'item'=>$collection->getTable('sales/order_item')), 
      'item.order_id=`main_table`.entity_id AND item.product_type="simple"', 
      array(
       'skus' => new Zend_Db_Expr('group_concat(item.sku SEPARATOR ", ")'), 
       'name' => new Zend_Db_Expr('group_concat(item.name SEPARATOR ", ")') 
      )); 

$this->addColumn('skus', array(
      'header' => Mage::helper('sales')->__('SKU'), 
      'index' => 'skus', 
      'type' => 'text', 
     )); 

     $this->addColumn('name', array(
      'header' => Mage::helper('sales')->__('NAME'), 
      'index' => 'name', 
      'type' => 'text' 
     )); 
相關問題