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.
Page 1 of 1
Multiple Related Table Contents In Single Cgridview
#2
Posted 24 January 2013 - 11:10 AM
first you should declare a relation for latest comment on messsage model relations method as this:
and a relation for user in Comment model
then you can access their values on your gridview
'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;',
),
),
));
#3
Posted 25 January 2013 - 12:45 AM
Reza m, on 24 January 2013 - 11:10 AM, said:
first you should declare a relation for latest comment on messsage model relations method as this:
and a relation for user in Comment model
then you can access their values on your gridview
'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
Share this topic:
Page 1 of 1

Help













