4

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'));

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");

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 .

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Hafiz
  • 4,187
  • 12
  • 58
  • 111
  • Your title doesn't really match your question. Are you trying to load multiple models in a view or trying to change what model is loaded? – udjamaflip Aug 24 '11 at 01:28
  • I was trying to load multiple models and was using a different model than default in view – Hafiz Aug 24 '11 at 02:41

1 Answers1

9

First approach

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            
}

Alternative approach

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'); 
}
hbit
  • 959
  • 2
  • 13
  • 33
  • how do you get a variable from that model?. `$this->var = $this->get('Msg');` How do I tell it where to search the function getMsg? – ValRob Oct 26 '18 at 10:43