2011-08-23 28 views
4

我正在使用的Joomla比視圖的自己的模式不同的模式,即由控制器分配給它,就像類似於它的名字:如何的Joomla MVC組件中使用多種模型

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

現在,我怎麼使用它方法getSingleUser($ user_id)在視圖中。在的Joomla文檔中的一個例子,它使用的是一些這樣的事:

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

所以我認爲數據1是MODEL2方法的名字嗎?如果是這樣,那麼我怎麼能通過在我的情況下是userid的參數。我知道很多joomla開發者都這麼做很容易,但我是各種開發者的傑作,也是joomla的新手,所以我希望你們能告訴我。

+0

您的標題並沒有真正符合您的問題。您是否試圖在視圖中加載多個模型或試圖更改加載的模型? – udjamaflip

+0

我正在嘗試加載多個模型,並且在視圖中使用了與默認不同的模型 – Hafiz

回答

9

第一種方法

我通過修改控制器如下這樣做(這是用戶控制器)

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'); 
} 
相關問題