sum()函数在cakephp查询 [英] sum() function in cakephp query

查看:222
本文介绍了sum()函数在cakephp查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在那里我使用这个查询,但它不返回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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆