gpt4 book ai didi

joomla - 如何在 joomla MVC 组件中使用多个模型

转载 作者:行者123 更新时间:2023-12-04 23:07:47 25 4
gpt4 key购买 nike

我在 joomla 中使用了不同的模型,而不是 View 自己的模型,通过从 Controller 分配它,它类似于它的名称,例如:

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

现在我如何在 View 中使用它的方法 getSingleUser($user_id) 。在 joomla 文档的一个例子中,它使用的是这样的东西:
$this->get("data1","model2");

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

最佳答案

第一种方法

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

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
}

在 View 中,您可以简单地对模型进行操作
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
}

替代方法

在 View 中直接加载模型:
function display($tpl = null){
// assuming the model's class is MycomponentModelBills
// second paramater is the model prefix
$actionsModel = & JModel::getInstance('bills', 'MycomponentModel');
}

关于joomla - 如何在 joomla MVC 组件中使用多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7159536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com