2

I want a view to call 2 different models for use.

Controller.php

class StatsController extends JController {
    function display()
     {
        if( !JRequest::getVar( 'view' ) ) {
            JRequest::setVar('view', 'stats' );
        }
        parent::display();
    }
    ...
    ...
}

Stats view : (index.php?option=com_stats&view=stats)

class StatsViewStats extends JView
{
    function display($tpl = null)
    {
        $model_helpdesk = & JModel::getInstance('Helpdesk','StatsModel');
    //$model_chart =  & JModel::getInstance('Chart','StatsModel'); 
    //$model_chart =  &$this->getModel('Chart');
    var_dump($model_chart);
    ...
    ...
        parent::display($tpl);
    }
}

Problem : getting the Helpdesk model works fine, but getting the Chart model either returns a blanc page , or returns null in var_dump. How can i get this second model for use (without modifying the controller) ??

Nidhal Rouissi
  • 325
  • 2
  • 6
  • 17

1 Answers1

3

In your controller, you'll need to do the following:

$view = &$this->getView('Stats', 'html');
$view->setModel($this->getModel('Stats'), true);
$view->setModel($this->getModel('Chart'));
$view->setModel($this->getModel('Helpdesk'));
$view->display();

Then you can access each model using the following:

$chartModel = $this->getModel('Chart');
$helpdeskModel = $this->getModel('Helpdesk');

Source

Matt Beckman
  • 5,022
  • 4
  • 29
  • 42
  • Sorry, but i already saw that & it doesn't help ! I just need to keep my controller's display function untouched so i can access the view directly via **index.php?option=com_stats&view=XXXX** – Nidhal Rouissi Mar 14 '12 at 22:27
  • Where in the controller should this be? In which method? – Felix G. Jan 14 '13 at 16:32
  • OK, I found out. It should be in the display() method in the Controller StatsController (in this case). Thanks :) – Felix G. Jan 14 '13 at 16:48