Yii Framework Forum: Multiple Related Table Contents In Single Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Multiple Related Table Contents In Single Cgridview Rate Topic: -----

#1 User is offline   Shobhith C 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 10-January 13
  • Location:Cochin

Posted 24 January 2013 - 09:46 AM

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.
0

#2 User is offline   Reza m 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 07-March 12

Posted 24 January 2013 - 11:10 AM

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;',
	        ),
        
	), 
    
)); 

0

#3 User is offline   Shobhith C 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 10-January 13
  • Location:Cochin

Posted 25 January 2013 - 12:45 AM

View PostReza 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:
'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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users