access related table in joined table

Got a strange thing here, when I use CGridView to show results from two joined table, say student table and registration table. when I access the fields from registration table.

‘$data->registrations->course_id’ doesn’t work, however '$data->registrations[0][‘course’] works.

registrations is the relation defined inside student model.

Anybody have the same problem here ?


It looks like that the registrations is a HAS_MANY releation, you can either change to has one or use the solution you found.

That is right, it is HAS_MANY. The thing is if I use $data->registrations[0][‘course_id’], when I sort the grid, if will generate error complain about undefined index 0.

Is there another way to fix this.