第一种方法
我通过如下修改控制器来做到这一点(这是用户的控制器)
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
}
在视图中,您可以简单地对模型进行操作
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
}
替代方法
在视图中直接加载模型:
function display($tpl = null){
// assuming the model's class is MycomponentModelBills
// second paramater is the model prefix
$actionsModel = & JModel::getInstance('bills', 'MycomponentModel');
}