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 p.id = t.id) As taskCount ', ));

		$criteria->together = TRUE;


		return new CActiveDataProvider('Firm', array(

			'criteria'=>$criteria,

		));



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(p.id) As taskCount';

$criteria->join = 'LEFT JOIN project p ON p.firm_id=t.id';

$criteria->group = 't.id';

...



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