How To Get Record Count When Getting Dataprovider

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 


How I am trying to do this?

I am trying to use ‘zii.widgets.grid.CGridView’ so I have:

$crewProvider = new CActiveDataProvider('Crew', array(


				'select'=>'*, COUNT(`id`) AS crew_count',

				'condition'=>'user_ship_id = "' . $id . '"',

				'group'=>'crew_type_id, status_id',

				'order'=>'crew_type_id ASC',






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.

Hi Deltawar, welcome to the forum.

Did you define a property named "crew_count" in your Crew model?

class Crew extends CActiveRecord



public $crew_count;


Hi softark, thanks for the welcome…

I have yes, I took a break & came back now it seems to be working, think it was not due some some caching or something.

Thank you for replying though, I greatly appreciate it.