show count() of related records in a CGridView column

in the firm/company grid view i want to show the running projects count for each firm in a column.

how can i show the ‘taskCount’ column in the CGridView?


		$criteria->with = array('projects'=>array(

			'select'=>'(select count(*) from project p where = As taskCount ', ));

		$criteria->together = TRUE;

		return new CActiveDataProvider('Firm', array(



i can do it like this

		'value'=> 'Project::model()->count("firm_id=".$data->id)',

but it fires separate sql query for each row.

I’m not sure what you are exactly trying to do, but it looks like you need something similar to (instead of with):


$criteria->select = '*, count( As taskCount';

$criteria->join = 'LEFT JOIN project p ON';

$criteria->group = '';


this is good, it worked! with 1 addition: should declare ‘taskCount’ in the firm model.