Hi all, I have been searching all corners of the internet and cannot seem the best practice for yii to simply get the results of this query.
SELECT * , COUNT( `id` ) AS crew_count
FROM `ship_crew` `t`
WHERE user_ship_id = "1"
GROUP BY crew_type_id, status_id
ORDER BY crew_type_id ASC
LIMIT 20
How I am trying to do this?
I am trying to use ‘zii.widgets.grid.CGridView’ so I have:
$crewProvider = new CActiveDataProvider('Crew', array(
'criteria'=>array(
'select'=>'*, COUNT(`id`) AS crew_count',
'condition'=>'user_ship_id = "' . $id . '"',
'group'=>'crew_type_id, status_id',
'order'=>'crew_type_id ASC',
),
'pagination'=>array(
'pageSize'=>20,
),
));
All the attributes of the ‘select’ get mapped to $data->attributes, but not the COUNT(id
) AS crew_count.
How can I get the count of each group? as I am grouping by ‘crew_type_id, status_id’, any count() I try simply returns the count of ALL RECORDS.
I have been stuck on this for ages and any help would be great.