How to use two models in one view - Joomla 3

I want to create a component based on com_weblinks.

This component will display categories and links on one page.

In 3.0, I don't understand how I can use 2 models (category models and link models) in one view.

+3


source to share


1 answer


First approach

I did this by changing the controller as follows (this is the controller for the 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 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            
}

      

An alternative approach

In the view, load the model directly:

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

      

+5


source







All Articles