当添加名称和Skus列时,Magento销售订单网格显示不正确的记录数 [英] Magento Sales Order Grid shows incorrect number of records when added Names and Skus columns

查看:123
本文介绍了当添加名称和Skus列时,Magento销售订单网格显示不正确的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Magento 1.4版本,并且向Sales Order Grid添加了额外的网格列(名称和skus),返回的数据是正确的,但我在分页和总记录数方面存在问题,我的代码如下:

首先编辑Mage_Adminhtml_Block_Sales_Order_Grid

 受保护的函数_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);
返回parent :: _ prepareCollection();
}

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

 受保护的函数_addColumnFilterToCollection($ column)
{
if($ this-> getCollection()& amp; ;& $ 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()
- > ;('find_in_set(?,skus)',$ column-> getFilter() - > getValue());

返回$ 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());

返回$ this;
}
}
return parent :: _ addColumnFilterToCollection($ column);
}

然后我在Mage_Sales_Model_Mysql4_Order_Collection class中编辑了这个方法getSelectCountSql() b
$ b

  public function getSelectCountSql()
{
$ countSelect = parent :: getSelectCountSql();

//增加
$ countSelect->重置(Zend_Db_Select :: HAVING);
// end

$ countSelect-> resetJoinLeft();
返回$ countSelect;
}

任何想法如何计算行数?

解决方案

也许它有点晚,但在你的代码中尝试使用GROUP insted的HAVING:

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

因为您使用的是这个人:

  $收藏 - > getSelect() - 基团( 'ENTITY_ID'); 


I'm working with Magento version 1.4 and I added extra grid columns (names and skus) to Sales Order Grid, the returned data is correct but I'm having problems with pagination and total number of records, my code as follow:

First I Edited 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();
}

Then I override this method to return correct results when filter by names or 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);
}

Then I edited this method getSelectCountSql() in Mage_Sales_Model_Mysql4_Order_Collection class

public function getSelectCountSql()
{
    $countSelect = parent::getSelectCountSql();

    //added 
    $countSelect->reset(Zend_Db_Select::HAVING);
    //end

    $countSelect->resetJoinLeft();
    return $countSelect;
}

Any Idea how can I calculate number of rows? Thanks in Advance.

解决方案

Maybe its a bit to late but in your code try using GROUP insted of HAVING:

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

Because you are using this statemen:

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

这篇关于当添加名称和Skus列时,Magento销售订单网格显示不正确的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆