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

查看:128
本文介绍了如何在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是model2中方法的名称?如果是这样,那么在我的情况下如何传递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天全站免登陆