cakephp 查询中的 sum() 函数 [英] sum() function in cakephp query
问题描述
我正在使用此查询,但它没有返回 ctotal
.请帮忙.
I am using this query, but it is not returning ctotal
. Please help.
$total = $this->RequestedItem->find('all',
[
'sum(cost * quantity) AS ctotal',
'conditions' => [
'RequestedItem.purchase_request_id' => $_GET['po_id']
]
]
);
推荐答案
您不应该直接在 CakePHP 中使用 PHP 超全局变量.您应该改用 Model.field
命名,以免出现不明确的字段错误.
You should not be using PHP superglobals directly in CakePHP. You should instead use Model.field
naming so that you do not get ambiguous field errors.
虚拟字段是可行的方法,但这不是您的问题,您需要多阅读本书.
Virtual fields is the way to go but that is not your problem, you need to read the book some more.
$total = $this->RequestedItem->find('all', array(array('fields' => array('sum(Model.cost * Model.quantity) AS ctotal'), 'conditions'=>array('RequestedItem.purchase_request_id'=>$this->params['named']['po_id'])));
应该可以正常工作,使用 virtualFields 就可以了
should work fine, with the virtualFields it would be
var $virtualFields = array('total' => 'SUM(Model.cost * Model.quantity)');
$total = $this->RequestedItem->find('all', array(array('fields' => array('total'), 'conditions'=>array('RequestedItem.purchase_request_id'=>$this->params['named']['po_id'])));
字段放在字段"键中,就像条件放在条件"键中一样.见 http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find
Fields go in the 'fields' key, just like conditions go in the 'conditions' key. See http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find
这篇关于cakephp 查询中的 sum() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!