Using Multiple models in a single view

Hi Guys.

I’m rebuilding one of my sites and I’m using Yii framework for the first time.

In a couple of hours I could do so much that I’m really a fan of Yii and its easyness to code to.

But now, I’m facing a problem and I’m asking here so that someone can put some light on my path.

I’m rebuilding this old page: mypsntrophies.com/profile/KlotX into Yii, and I have 3 database tables with their respective relationship tables supporting this page (user, user_trophies, trophies, user_games, games) , but to translate it to MVC, I’m not getting to the point on how to do this.

So, I need to have 3 models (?) being loaded in one controller to be presented in one view.

Which would be the best way to do it ?

Thanks in advance.

Regards,

Nuno

You can create any number of models in the controller method and pass them to the view like




$user = "get user data";

$user_trophies="get user trophies data"

$this->render("yourview", array('user'=>$user, 'user_trophies' => $user_trophies) );

Depending on your model relations… maybe you even don’t need to make all this…

For example if you have defined the relation "trophies" in the user model… then you can use $user->trophies to get the user_trophies data…

Read more about relations if you have not done already - http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Hi mdomba,

This looks exactly like what I was looking for :)

And defining relations might be exactly the perfect solution for this, I just need to test it out since I don’t want to get all the user relations (trophies, games, etc) when for instance getting only the user information, nevertheless, it might also be a different model.

I’ll try to come up with a solution and then I’ll post here if it worked or not.

Thanks a lot, you were very helpful.

Nuno

Just a note…

you can define all the relations in the user model… this does not mean that all those data will be retrieved…

The actual query (data retrieval) will be made only when you explicitly ask for it with $user-<name of relation>

Hi mdomba.

It’s nice that you point that out, I was reading the exact same thing on the link you provided. This is a really awesome feature, it perfect…

I’ll let you all know if this worked…

Thanks once again

Hi all

Maybe this thread is not in the right place, but that a detail.

I’m having one problem with this.

So I created the following relations in both Users and Games models:




return array(

			'games' => array(self::MANY_MANY, 'Games','games_users(psnId,titleId)'),

		);


(...)

return array(

			'users' => array(self::MANY_MANY, 'Users','games_users(titleId,psnId)'),

		);



And now in my controller I want to do the following:




$user = $this->loadModel($id);

		

		$this->render('view',array(

			'model'=>$user);

		));



And in my view I want to use a CDetailView to show the $model and a CGridView to show $model->games, but as $model->games is an array I don’t know how to put it as a CActiveDataProvider and build the CGridView.

What do I need to do ?

Thanks

For new problems it’s better to open new threads…

There is the CArrayDataProvider that you can use - http://www.yiiframework.com/doc/api/1.1/CArrayDataProvider

Or use a CActiveDataProvider but in this case you dont use the relation…