sum()函数在cakephp查询 [英] sum() function in cakephp query
问题描述
在那里我使用这个查询,但它不返回ctotal请帮助:
$ total = $ this- > RequestedItem-> find('all',
array('sum(cost * quantity)AS ctotal','conditions'=> array('RequestedItem.purchase_request_id'=> $ _ GET ['po_id '])));
您不应该直接在CakePHP中使用PHP superglobals。您应该使用 Model.field
命名,这样您就不会产生不明确的字段错误。
虚拟字段但这不是你的问题,你需要再读一下这本书。
$ 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']));
$ b b
应该正常工作,与virtualFields是
var $ virtualFields = array('total'=> 'SUM(Model.cost * Model.quantity)');
$ total = $ this-> RequestedItem-> find('all',array(array('fields'=> array '),'conditions'=> array('RequestedItem.purchase_request_id'=> $ this-> params ['named'] ['po_id'])));
字段放在'fields'键中,就像条件键中的条件一样,参见http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find
hi there i am using this query but it is not returning the ctotal Please help:
$total = $this->RequestedItem->find('all', array('sum(cost*quantity) AS ctotal', 'conditions'=>array('RequestedItem.purchase_request_id'=>$_GET['po_id'])));
解决方案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'])));
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'])));
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
这篇关于sum()函数在cakephp查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!