Multiple Related Table Contents In Single Cgridview

Hi,

i have 3 tables,

#1

message


id(PK)

title

content

user_id

date

status

#2

comment


id(PK)

message_id(FK)

comment

user_id(FK)

date

#3

user


id(PK)

name

username

password

There is a 1 to many relation between ‘message’ and ‘comment’ .In message view I want to show “message.id,message.title, latest commented user.name,latest comment.date and message.status” in single CGridView.Please help me…

Thanks.

first you should declare a relation for latest comment on messsage model relations method as this:




'latestComment'=>array(self::HAS_ONE, 'Comment', 'message_id', 'order'=>'date desc'),



and a relation for user in Comment model




'user' => array(self::BELONGS_TO, 'User', 'user_id'),



then you can access their values on your gridview




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

	'id'=>'message-grid',

	'dataProvider'=>$model->search(),

    'columns'=>array(

        'id',

        'title',

        'status',

        array(

	        'header'=>'Latest Commented User',

                'value'=>'$data->latestComment->user->name;',

	        ),

        

        array(

	        'header'=>'Latest Comment Date',

                'value'=>'$data->latestComment->date;',

	        ),

        

	), 

    

)); 



Thanks Reza…

Unfortunately its not working for me :( .commented user and comment date listed as empty