Jonathon Posted February 4, 2013 Share Posted February 4, 2013 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 More sharing options...
Edward Posted February 4, 2013 Share Posted February 4, 2013 Why not put both of the queries into the model company in just one method then use a return array to return both values. Otherwise what you are doing is correct. 1 Link to comment Share on other sites More sharing options...
Jonathon Posted February 4, 2013 Author Share Posted February 4, 2013 Ok, i'll look at doing that. The main thing for me is that you can use them this way. Thanks Link to comment Share on other sites More sharing options...
Recommended Posts