joomla – using multiple models per view
Quite often Joomla views need information from more then one model. It is a bit unclear from the joomla documentation how to achieve this.
In a Joomla View Class, a call to:
$defaultModel = $this->getModel()
retrieves the associated default model instance. Joomla expects the model to have the same name as the view.
To retrieve another model instance it is possible to pass a model name as parameter:
$otherModel = $this->getModel('myothermodel')
But without additional instructions this will lead to an error because the view does not yet know about “myothermodel”. Before the above code will return a valid model, the view needs first to be informed about this model.
The only logical place to do this is in the controller class. Before instructing the controller to display the view in question, add the following lines:
$view = $this->getView('myview', 'html') ;
$view->setModel( $this->getModel( 'myothermodel' )) ;
replace “myview” and “myothermodel” with the appropriate names and now the view
should be able to retrieve a valid instance of “myothermodel”
one important thing to note:
I could only get it to work when the actual model filename is all lowercase.
E.g. MyModel.php could not be recognized by Joomla’s getModel method on my Linux system.
Only after changing the filename to mymodel.php is would work.