Display Two Tables in View

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

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 should work ::)

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

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?>

I GOT IT!! thanks for the help guys!