Jump to content
Larry Ullman's Book Forums

Passing Multiple Models.


Jonathon
 Share

Recommended Posts

Hi,

 

I have been just playing around with Yii and querying Databases. A couple of things struck me in the last few minutes. I've been trying to get all the information I want to get over to the view in one fell swoop (1 model). Which is what I think hindered my initial work.

 

For instance this is some code from one of my controllers

 

 
    public function actionIndex($id)
    {
 
    $companyItems = Company::model()->returnCompanyItemListings($id); // This model method returns the items listed by a company
    
        $this->render('index', array(
        'model'=>$companyItems,
              ));
    }
 

 

It works fine and returns the information I want back. (It's actually a fairly complex three table join using DAO). Then ran into a similar problem as before. I can only use this information once in the foreach loop. I then remembered some code in another controller about loading models that I used. (It's actually the code for loadModel().

 

I then updated my code in the controller to this:

	public function actionIndex($id)
	{

	$companyItems = Company::model()->returnCompanyItemListings($id); // This model method returns the items data
	
	$model = Company::model()->findByPk($id);

		$this->render('index', array(
		'model'=>$companyItems,
		'model2'=>$model,
		));
	}

 

 

So by passing 2 models I can use one $model2->name to echo out the company on the company listing page and it will be dynamically adjusted. I can then use $model in the foreach loop to get the items data. Is that acceptable?? Because if so. It's been a decent discovery for me and something I hadn't put together from the book.

 

Jonathon

Link to comment
Share on other sites

 Share

×
×
  • Create New...