gpt4 book ai didi

php - Yii 框架 : get sum by scope

转载 作者:行者123 更新时间:2023-12-04 04:38:23 26 4
gpt4 key购买 nike

我的用户模型中有范围:

public function scopes()
{
return array(
'sumPrice'=>array(
'select'=>'SUM(`price`)',
),
);
}

我想在 Controller 中获取 sumPrice,但我不知道有什么方法可以做到这一点。我试过:

$sumPrice=User::model()->sumPrice(); //it returns empty record (user model filled by NULLs)

$sumPrice=User::model()->sumPrice()->count(); //it returns count records in user

$sumPrice=User::model()->sumPrice()->findAll(); //it returns all records in user



但是没有返回和的函数,那么如何得到它呢?

已解决:
$sumPrice = Yii::app()->db->createCommand('SELECT SUM(`price`) AS `sum` FROM `user`')->queryAll();
var_dump($sumPrice[0]['sum']);

最佳答案

您需要对事件记录进行统计查询,在模型上定义关系

 class Post extends CActiveRecord
{
public function relations()
{
return array(
'commentCount'=>array(self::STAT, 'Comment', 'post_id'),
'categoryCount'=>array(
self::STAT, 'Category', 'post_category(post_id, category_id)'
),
);
}
}

Yiiframework

关于php - Yii 框架 : get sum by scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19332046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com