Yii Framework Forum: Display Two Tables in View - Yii Framework Forum

Jump to content

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

Display Two Tables in View Rate Topic: -----

#1 User is offline   mtokoly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 22-October 10

Posted 24 October 2010 - 07:00 PM

Hi all,

I'm trying to figure out how to display two tables in one view. I have a POSTS table and a USERS table. On the actionIndex(), I would like to show the post (title and excerpt) and the user (name). Currently for the controller PostsContoller I have:

public function actionIndex() {
		
    $dataProvider = new CActiveDataProvider('PostsModel');
    $this->render('index', array(
        'dataProvider' => $dataProvider,
    ));
}


And honestly, I have no idea on where to go from there. What I trying to create is a:

SELECT * FROM tbl_posts JOIN tbl_users ON tbl_users.user_id = tbl_posts.post_author


And display the results. Please help
0

#2 User is offline   binkabir 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 193
  • Joined: 25-July 10
  • Location:Abuja,Nigeria

Posted 25 October 2010 - 01:18 AM

Hello, first u need to create a relation in the POST AR that points to the user

eg 'author'=>array(self::BELONG_TO,'User','post_author')

when u do this u can use the 'author' as the means to join the 2 table and fetch the corresponds Match values

then create your CActiveDataProvider in your Post AR like this

public function getPostModel(){

return new CActiveDataProvider(get_class($this));
}


in your controller method

public function actionIndex(){

$dataProvider = POSTMODEL::model()->with('author')->getPostModle();

$this->render('index',array('dataProvider'=>$dataProvider));
}

this should work ::)

please read the blog example that comes with the installation package, all this are mentioned.
0

#3 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 25 October 2010 - 01:19 AM

In your model posts you should have a relation to users.

So you can customze your _view (the one that shows the single item of the list) with something like that:

author: <?php echo $data->user->username?>

0

#4 User is offline   mtokoly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 22-October 10

Posted 25 October 2010 - 11:55 AM

I GOT IT!! thanks for the help guys!
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