How to use two models in one view - Joomla 3
1 answer
First approach
I did this by changing the controller as follows (this is the controller for the user)
function doThis(){ // the action in the controller "user"
// We will add a second model "bills"
$model = $this->getModel ( 'user' ); // get first model
$view = $this->getView ( 'user', 'html' ); // get view we want to use
$view->setModel( $model, true ); // true is for the default model
$billsModel = &$this->getModel ( 'bills' ); // get second model
$view->setModel( $billsModel );
$view->display(); // now our view has both models at hand
}
In the view, you can simply do your operations on the models
function display($tpl = null){
$userModel = &$this->getModel(); // get default model
$billsModel = &$this->getModel('bills'); // get second model
// do something nice with the models
parent::display($tpl); // now display the layout
}
An alternative approach
In the view, load the model directly:
function display($tpl = null){
// assuming the model class is MycomponentModelBills
// second paramater is the model prefix
$actionsModel = JModel::getInstance('bills', 'MycomponentModel');
}
+5
source to share