如何在 joomla MVC 组件中使用多个模型 [英] How to use multiple models in joomla MVC Component

查看:20
本文介绍了如何在 joomla MVC 组件中使用多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 joomla 中使用了与视图自己的模型不同的模型,该模型与它的名称相似,是从控制器分配的,例如:

I am using different model in joomla than view's own model that is similar to its name by assigning it from controller, like:

         $view->setModel($this->getModel('user'));

现在我如何在视图中使用它的方法 getSingleUser($user_id).在 joomla 文档的一个例子中,它使用的是这样的东西:

Now how can I use its method getSingleUser($user_id) in view. In an example in joomla documentation , it is using is some thing like this:

$this->get("data1","model2");

所以我假设 data1 是模型 2 中的方法名称?如果是这样,那么在我的情况下如何传递 userid 的参数.我知道这是许多 joomla 开发人员都做过的简单事情,但我是各种开发人员的杰克,也是 joomla 的新手,所以我期待你们告诉我.

So I assume data1 is name of method in model2? If so then how can I pass argument that is userid in my case. I know this is easy thing that many of joomla developers have done but I am sort of jack of all sort of developer and new to joomla so I am expecting you guys to tell me .

推荐答案

第一种方法

我通过如下修改控制器来做到这一点(这是用户的控制器)

I did this by modifying the controller as follows (this is the controller for user)

function doThis(){ // the action in the controller "user" 
    // We will add a second model "bills"
    $model = $this->getModel ( 'user' ); // get first model
    $view  = $this->getView  ( 'user', 'html'  ); // get view we want to use
    $view->setModel( $model, true );  // true is for the default model  
    $billsModel = &$this->getModel ( 'bills' ); // get second model     
    $view->setModel( $billsModel );             
    $view->display(); // now our view has both models at hand           
}

在视图中,您可以简单地对模型进行操作

In the view you can then simply do your operations on the models

function display($tpl = null){              
    $userModel = &$this->getModel(); // get default model
    $billsModel = &$this->getModel('bills'); // get second model

    // do something nice with the models

    parent::display($tpl); // now display the layout            
}

替代方法

在视图中直接加载模型:

In the view directly load the model:

function display($tpl = null){
 // assuming the model's class is MycomponentModelBills 
 // second paramater is the model prefix    
        $actionsModel = & JModel::getInstance('bills', 'MycomponentModel'); 
}

这篇关于如何在 joomla MVC 组件中使用多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆