2012-01-06 54 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' 
     )); 
相关问题