添加名称和Skus列时,Magento销售订单网格显示错误的记录数

Abo*_*ed1 5 grid zend-framework magento

我正在使用Magento 1.4版,我在Sales Order Grid中添加了额外的网格列(名称和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();
}
Run Code Online (Sandbox Code Playgroud)

然后我重写此方法,以便在按名称或skus过滤时返回正确的结果

    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);
}
Run Code Online (Sandbox Code Playgroud)

然后我在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;
}
Run Code Online (Sandbox Code Playgroud)

任何想法如何计算行数?提前致谢.

Mea*_*bed 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;
}
Run Code Online (Sandbox Code Playgroud)

为了实现网格存储我已经覆盖

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;
    }
Run Code Online (Sandbox Code Playgroud)

并将列的filter_index设置为

 in _prepareColumns() function 
    'filter_index' => 'SUM(tablename.field)'
Run Code Online (Sandbox Code Playgroud)

您可以在列的过滤器上使用回调函数