Multiple models for a view without controllers

两盒软妹~` 提交于 2019-12-25 06:37:48

问题


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



来源:https://stackoverflow.com/questions/15804408/multiple-models-for-a-view-without-controllers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!