You are here: cakephp » using different models in a single(1) controller
Using different models in a single(1) controller
- Written By
- PHPin24
- Submitted At
- 2009-07-28 09:39:34
- Num Views
- 609
- Category
- CakePHP
|
**BEST SOLUTION** You should use database relations (set relations in Models)to navigate $this->Client->SecondaryModel->find() or alternatively you can use containable (only available from > Cake 1.1). Or your best bet would be to use $this->loadModel when you need it. $this->loadModel('SecondaryModel'); ********** If you receive an error after executing above command like 'javascript' not available or any weird error. Ensure you have setup your model properly by linking it to a database <?php class PublicHoliday extends AppModel{ var $useDbConfig = 'default'; } ?> and ensure your default database is connecting and setup correctly in app/config/database.php ******** If no errors you can now access the model like this: $this->SecondaryModel->findAll(); **ALTERNATIVE SLOW METHOD BELOW** WARNING: This loads the model everytime the controller is used and can slow your site down a lot! In your controller just after the class ModelName definition Add the variable var $uses = array('MainModel' , 'SecondaryModel'); class ClientController extends AppController { var $helpers = array('javascript'); var $uses = array('MainModel','SecondaryModel'); In the controller then to use the model like this $this->MainModel and then like you would usually reference stuff. ex. $this->MainModel->save(); By PHPin24 @ 2009-07-28 09:39:34
|
