in Yii's criteria how to get count (*)
17,864
Solution 1
Work's with alias. Yii 1.1.11. Fails other
$criteria= new CDbCriteria();
$criteria=array(
'group'=>'attribute',
'select'=>'id,count(*) AS cc'
);
Solution 2
This not exactly an answer to your question, but could still be useful for you. If you use the result to only look up one count for one specific attribute, you don't need to fecth an entire array, but use Yii's count
method:
$criteria= new CDbCriteria();
$criteria.compare('col_name', $desired_col_value);
$count = X::model()->count($criteria);
Author by
Hilmi
Updated on June 05, 2022Comments
-
Hilmi almost 2 years
I'm trying to build a query that has a
group by
attribute, im truing to get theid
and thecount
it keeps telling me that thecount (*)
is invalid column name how could i get thecount
from thegroup by
query ?? -
psycho brm over 10 yearsSo how do I access it then?
$x = X::model()->findAll(...); $x[0]->cc
is not working.. -
psycho brm over 10 yearsIt does if I add the variable into the model:
class X .. { public $cc;
. After that, the$x[0]->cc
works.