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

标签 magento zend-framework grid

我正在使用 Magento 1.4 版,我向销售订单网格添加了额外的网格列(名称和 sku),返回的数据是正确的,但我在分页和记录总数方面遇到问题,我的代码如下:

首先我编辑了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类中编辑了这个方法getSelectCountSql():

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

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

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

如何计算行数?

最佳答案

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

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

因为您正在使用此声明:

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

关于添加名称和 Skus 列时,Magento 销售订单网格显示不正确的记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8764543/

相关文章:

javascript - 未捕获的类型错误 : undefined is not a function when using a jQuery plugin in Magento

mysql - 从备份中恢复 Magento 静态 block 内容?

php - 在 Magento 1.9.1.1 中重新索引类别产品时出现问题

Python Tkinter - 在窗口中均匀调整小部件的大小

javascript - Sencha touch 2 中的网格

php - Magento 订单 ID 重新排序

magento - 如何使单个管理员只能访问单个商店产品

php - php文件中的8个空格如何使apache崩溃?

php - Zend Framework 2 和 Doctrine 2 - 多个数据库的配置

html - Bootstrap 3 网格工作正常,在 1170px 左右,所有列都像具有 "display: block;"属性的 col-lg-12