Search code examples
joomla2.5joomla-extensions

Joomla: calling function from models with parameter


Since Joomla 2.5 I can call functions from the default model now with this code:

$result = $this->get('Data');

Where get and Data together are leading to the function name "getData". But I cannot do this:

$myModel = $this->getModel('special_model');
$result = $myModel->getData();

and I can also not do:

$myModel = $this->getModel('special_model');
$result = $myModel->getData('myId');

So, I can't call a method directly and also not with a parameter? Is it correct? Why is it like this? In J!1.5 this was possible.

Best Regards Björn


Solution

  • What you have actually will work in J2.5, but you have to add one more piece. That should work as is if you are in the 'special_model' view. Based on the naming though, you are trying to add a second model to the view. This model has to be added to the view from the controller:

    $view = $this->getView('myview', 'html') ;
    $view->setModel( $this->getModel( 'special_model' )) ;
    

    Again, add that to the controller, and the code you have will work in the model.