0

我已将类别过滤器添加到正在工作的管理网格中,下面是代码

$cat_ids = implode(',', $CatIdArray['CategoryId']);
$collection->getSelect()
         ->join(array('ccp'=>'catalog_category_product'),
            "ccp.product_id = e.entity_id AND ccp.category_id IN({$cat_ids})",
            array('category_id')
         );

现在的问题是当多个类别具有相同的产品时它会崩溃,以避免我使用

$collection->groupByAttribute('entity_id');

这给我带来了不同的记录,但这打破了分页。我知道这是常见问题,但任何帮助将不胜感激。谢谢

4

2 回答 2

4

如果您面临分组问题。我使用这种方式多次解决相同的问题。从 magento/lib/Varien/Data/Collection/Db.php 复制 Db.php 文件将其粘贴到您的本地目录,因此生成的文件夹结构将如下所示: magento / 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);

        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;
    }.Try this , hope this will help you.
于 2013-06-20T03:55:57.803 回答
3

代替

$collection->groupByAttribute('entity_id');

用这个

$collection->getSelect()->group('entity_id');
于 2013-06-27T08:23:12.700 回答