Multiple models for a view without controllers

2019-08-25 20:11发布

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...

标签: joomla
2条回答
再贱就再见
2楼-- · 2019-08-25 20:38

Yes, you can load any model in the view with

$model = JModel::getInstance('ModelName', 'ComponentNameModel');
查看更多
\"骚年 ilove
3楼-- · 2019-08-25 20:48

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...

查看更多
登录 后发表回答