Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento ORM中的聚合函数_Magento - Fatal编程技术网

Magento ORM中的聚合函数

Magento ORM中的聚合函数,magento,Magento,如何在Magento ORM中使用聚合函数,如sum()、count() 假设有一个带有以下值的“测试”表 ------------------------------------- productid | qty ------------------------------------- 1 | 2 ------------------------------------- 2

如何在Magento ORM中使用聚合函数,如sum()、count()

假设有一个带有以下值的“测试”表


-------------------------------------
productid           |          qty
-------------------------------------
1                   |            2
-------------------------------------
2                   |            3
-------------------------------------
3                   |            5
-------------------------------------
如果我在我的模块中映射了这个表,这样我就可以得到像


$_collection = Mage::getModel('mymodule/customcart')->getCollection();
如何使用集合获得与下面的查询等价的结果


select sum(qty) from test;

如果您的
mymodule/customcart
集合基于EAV,则可以使用

否则,您将不得不更直接地使用SQL:

$_collection = Mage::getModel('mymodule/customcart')->getCollection();
$_collection->getSelect()
    ->columns('SUM(item_qty) AS qty')
    ->group('productid');

将聚合集合与网格或寻呼机一起使用时要小心。它们依赖于不能很好地处理分组查询的函数,您可能必须提供自己的
getSelectCountSql
函数,该函数返回准确的计数。

@clockworkgeek如何在magento集合中尝试此查询:从test中选择sum(数量+产品ID-任何其他列)@VijayS如果你无法从上面得出答案,因为你的情况不同,那么请开始一个新问题,也许可以参考这个问题并解释它的不同之处。
$_collection = Mage::getModel('mymodule/customcart')->getCollection();
$_collection->getSelect()
    ->columns('SUM(item_qty) AS qty')
    ->group('productid');