php $criteria->在yii中选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7746186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$criteria->select in yii
提问by iThink
Is it possible to do the following in yii
是否可以在 yii 中执行以下操作
$criteria=new CDbCriteria;
$criteria->select='avg(rate) as avgRate,rate';
I executed the following code but its returning the value for the column "rate" but not "avgRate" I know this could be done by createcommand but I want to use CDbCriteria.
我执行了以下代码,但它返回“rate”列的值而不是“avgRate”我知道这可以通过 createcommand 完成,但我想使用 CDbCriteria。
回答by Mukesh Soni
try this
尝试这个
$criteria->select = array('rate', 'avg(rate) as avgRate');
And don't forget to define $avgRate
as a public variable in your model.
并且不要忘记$avgRate
在模型中定义为公共变量。
回答by ldg
You should be able to do this, but you will have to define $avgRate property in your model assuming you are using it that way.
您应该能够做到这一点,但您必须在模型中定义 $avgRate 属性,前提是您正在以这种方式使用它。
Also, I'm not sure if your example is literally what you want to do as the "rate" value won't have much use.
另外,我不确定您的示例是否真的是您想要做的,因为“速率”值没有多大用处。