【问题标题】:Magento grid, getting incorrect total valueMagento网格,总价值不正确
【发布时间】:2023-03-20 16:50:01
【问题描述】:

我创建了一个网格。问题是我得到的记录总数不正确。但所有记录都在网格中。 我在 grid.html $this->getCollection()->getSize() 代码中尝试过,它返回的值不正确。

但是 count($this->getCollection()) 返回正确的值。如何使用 $this->getCollection()->getSize() 解决这个问题。

谁能帮帮我。 谢谢你

【问题讨论】:

    标签: magento


    【解决方案1】:

    这是在您的集合中使用 group by 子句或 having 子句(或两者)的经典案例。 Magento 的 getSelectCountSql 函数不考虑 group by 或 having 子句,因为它的前端性能很差。相反,它通常使用指数和统计数据。但是,如果您愿意,可以按如下方式覆盖您收藏的 getSelectCountSql

    public function getSelectCountSql($select) {
        $countSelect = clone $select;
        $countSelect->reset(Zend_Db_Select::ORDER);
        $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
        $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
        if ($select->getPart(Zend_Db_Select::HAVING)) {
            //No good way to chop this up, so just subquery it
            $subQuery = new Zend_Db_Expr("(".$countSelect->__toString().")");
            $countSelect
                ->reset()
                ->from(array('temp' => $subQuery))
                ->reset(Zend_Db_Select::COLUMNS)
                ->columns('COUNT(*)')
            ;
        } else {
            $countSelect->reset(Zend_Db_Select::COLUMNS);
            // Count doesn't work with group by columns keep the group by
            if (count($select->getPart(Zend_Db_Select::GROUP)) > 0) {
                $countSelect->reset(Zend_Db_Select::GROUP);
                $countSelect->distinct(true);
                $group = $select->getPart(Zend_Db_Select::GROUP);
                $countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")");
            } else {
                $countSelect->columns('COUNT(*)');
            }
        }
        return $countSelect;
    }
    

    这应该从getSize返回正确的数字

    【讨论】:

    • 是的,由于 GROUP BY,它不起作用。我用另一种方式写了sql来解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多