How To Use Relational Active Record In Yii

Hi,

I have been working with yii for some time. But I still not got relations in active records correctly. I am using the yii blog site which is in demo folder in yii download.

I want to access a field of comments table. But I still not succeeded.

error:

Error 500

Object of class User could not be converted to string

I know this may be very trivial question. But if any one can help on this I would highly appreciate it.

hello nuvan you are getting the error because when you accessing the


$post->author

it fetches the post’s author object( that is you have relations specifed on the post model) so whenever you


echo $post->author

you will get this error because you are trying to print the author object. so if you want to access author’s data such as author’s name etc you can use like this


echo $post->author->username; echo $post->author->email; //etc

read http://www.yiiframework.com/wiki/428/drills-search-by-a-has_many-relation/

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#performing-relational-query

Thanks Ahamed and sefburhan for the replies. It is working now. And I will include other details of the issue because it may help some one else also.

ER diagram of db is attached with this.

Post Model

Comment model

PostController