Multiple models for a view without controllers

2019-08-25 20:24发布

问题:

As I've just started with Joomla component development, this might sound stupid, or might be trivial.

I would like to know if its possible to have different models attached to a view, without using separate controllers?

My intention is actually to use same model for different views.

Thanx in advance...

回答1:

Yes, you can load any model in the view with

$model = JModel::getInstance('ModelName', 'ComponentNameModel');


回答2:

OK, got it working. Basically you just need to check for the 'view' variable in the JRequest class:

if(JRequest::getVar('view') == 'yourtargetview') {
      $modelMain = $this->getModel ( 'yourtargetmodel' );
      $viewCallback  = $this->getView  ( 'yourtargetview', 'html');
      $viewCallback->setModel( $modelMain, true );  // true is for the default model;
    }

And then in your target view class, reference the model functions as follows(notice the second parameter to get call):

$this->targetFieldValue = $this->get('targetMethod', 'targetModel');

Hope it helps...



标签: joomla