Relational data CGridview


Before i state my question, I would liek to clarify, I have searched the forum, and found some post regarding this issue. I followed it as close as i can, however I am still left with the following issue:

I have 2 tables:

User - with the folllowing relationship defined in the model: ‘role’ => array(self::BELONGS_TO, ‘Role’, ‘id’)

and "Role" with no relation defined(thats what i understand from how BELONGS_TO works, as i read in another thread)

The user table has a column named role_id

The role table has an id and a name

in the gridview, i want a column to show the name of the role that belongs to a user.

This is what I currently have:


$criteria=new CDbCriteria;


$criteria->compare('',$this->role, true);






//load the related table at the same time:


return new CActiveDataProvider(get_class($this), array(



in my controller i have:

 $oUser = new User('search');


 $this->render('index', array(

      'oUser' => $oUser


and last, the view:

$this->widget('zii.widgets.grid.CGridView', array(

















The current error I get is trying to get property of non-object.

Altough the error is clear, I still find myself failing at finding the solution… :confused:

Anyone that could tell me what im doing wrong?

Thanks for taking the trouble of reading this :)

Shouldn’t your role relation be like


I have changed the relationships to

Role model: ‘user’ => array(self::HAS_MANY, ‘User’, ‘role_id’)

User model: ‘role’ => array(self::HAS_ONE, ‘Role’, ‘id’) (id == role_id)

But im used to giving every table "id" rather then role_id or user_id as primary key

Anyway the changes dont affect the problem, it still persists, the error is caused because of the search() in user, im quite sure of that. But why its caused im not sure. Somewhere im doing something wrong.

have you tried with what I said ?

Sorry, yes I have, it did nothing. :(

Ok I’ saying that bcause the id in the role relation should be the foreign key(role_id) and not the pramery key.

You’re saying that my role table should have an id and a role_id? :confused:

I don’t think we are understanding eachother.

heres the table structure im working with:

User table:

PK: id int

FK: role int

username varchar

password varchar

salutation varchar

firstname varchar

lastname varchar

Role table:

PK: id int

name varchar

It’s very possible im just being stupid. Eitherway, I have tried what you said, and it’s not working.

Hi Kyura!

The best advice I can give you about relations, is to use InnoDb and generate the relations with Gii.

If you did so, your relations are correct.

Maybe you have some user that has not a role associated, therefore try using in the grid this:


This will not give you error, can be an help for debugging.

Thank you very much.

That was exactly the problem, a user not having a role assigned.

Thanks again and have a nice remaining day! ^.^ I know i will!

I also wrote a bit of a workaround in the meantime, but I hope I can fix it with this bit of logic. I should be able to.