1

我正在尝试按类别过滤网格而不添加类别列,有没有一种方法可以让我们将此自定义过滤器添加到网格中。

下面是我所做的

function prepareCollection()
{
$catIdArray = getRequest()->getParam('cat_id');
//i.e  $catIdArray = array(26, 27, 17);

$collection = Mage::getModel('catalog/product')->getCollection(); 

$collection->joinField(
    'category_id',
    'catalog/category_product',
    'category_id',
    'product_id=entity_id',
    null,
    'left'
)
->addAttributeToFilter( 
    'category_id',
    array('in' => $catIdArray)
)
->addAttributeToSelect('*');
$this->setCollection($collection);

这里过滤器工作正常,但在这种情况下分页不起作用。请建议我我做错了什么???

4

1 回答 1

1

为了在 Magento 后台扩展网格后修复分页,您需要扩展lib/Varien/Data/Collection/Db.php

Db.php复制到路径app/code/local/Varien/Data/Collection/Db.php并将函数替换getSelectCountSql为以下内容:-

public function getSelectCountSql()
{   
    $this->_renderFilters();
    $countSelect = clone $this->getSelect();
    $countSelect->reset(Zend_Db_Select::ORDER);
    $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
    $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
    $countSelect->reset(Zend_Db_Select::COLUMNS);

    // Count doesn't work with group by columns keep the group by 
    if(count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) {
        $countSelect->reset(Zend_Db_Select::GROUP);
        $countSelect->distinct(true);
        $group = $this->getSelect()->getPart(Zend_Db_Select::GROUP);
        $countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")");
    } else {
        $countSelect->columns('COUNT(*)');
    }
    return $countSelect;
}
于 2013-06-20T08:38:36.273 回答