How to display count of records in grid


I have found an example in tutorial, but I must be doing something wrong as column with totals of participants is empty. Could you help me please?


public $participantTotal;

public function rules()


'participants' => array(self::MANY_MANY, 'Participant', 'ParticipantServices(ServiceId, ParticipantId)'),

'participantTotal'=>array(self::STAT, 'Participant',  

‘ParticipantServices(ServiceId, ParticipantId)’),);



<?php $this->widget(‘zii.widgets.grid.CGridView’, array(








I just took a very quick look but I would guess that you shouldn’t define a public property called participantTotal when you have a relation that is named exactly like that.

The magic getter method of CActiveRecord first returns properties of the model and if it don’t find one it searches for a relation named like that. So it will always stop at your public property which is null and possibly useless. Rename that one or rename the relation and it should work



P.S.: And by the way why is your relations() function “called” rules()? :) Typo or error? If so than I wonder that you can even see a GridView ;)

Thank you Haensel!

Yes, it was a typo and your tip resolved my problem.