Jump to content
Larry Ullman's Book Forums

Passing Multiple Models.


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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...