0

Possible Duplicate:
How to use multiple models in joomla MVC Component

In J!2.5 Tutorial there is just one model (the default one) loaded in the view. An the snipplets from J!1.5 doesn't work anymore together with the use of techniques from the tutorial. Here is the turoial stuff extended with mine. controller.php

    function display($cachable = false) 
{
    // set default view if not set
    $input = JFactory::getApplication()->input;
    JRequest::setVar('view', JRequest::getCmd('view', 'HelloWorlds'));

    // We will add a second model "bills"
    $model = $this->getModel ( 'helloworlds' ); // get first model
    $view  = $this->getView  ( 'helloworlds', 'html'  ); // get view we want to use
    $view->setModel( $model, true );  // true is for the default model  

    $billsModel = &$this->getModel ( 'mycomponents' ); // get second model   
    $view->setModel( $billsModel );             

    // call parent behavior
    parent::display($cachable);
}

And here is my view. views/helloworlds.view.hml.php

    function display($tpl = null) 
{
    // Get data from the model
    $model = $this->getModel();
    $items = $this->get('Items');
    $pagination = $this->get('Pagination');

    $model2 = &$this->getModel('mycomponents');
    $items2 = $model2->get('Items');
    $pagination2 = $model2->get('Pagination');

    // Check for errors.
    if (count($errors = $this->get('Errors'))) 
    {
        JError::raiseError(500, implode('<br />', $errors));
        return false;
    }
    // Assign data to the view
    $this->items = $items;
    $this->pagination = $pagination;
    $this->items2 = $items2;
    $this->pagination2 = $pagination2;

    // Display the template
    parent::display($tpl);
}

But unfourtunately, there is no model2 loaded. Whats going on here? Is my error in the controller? Do have set both models correct with in the controller? And what about my code in the view? Do I have to load the model in that way (known from J!1.5)?

Best Regards Björn

Community
  • 1
  • 1
DreiBaer
  • 61
  • 1
  • 8
  • I do not know why it is doubled. Please give me the link to the questions which might to be the duplicate. – DreiBaer Feb 28 '13 at 20:50

1 Answers1

0

I got it. Solution is to register your model in the controller (like I did already).

And then in the view just call it with

    $items2 = $this->get('Items', 'MyComponents');

instead of

    $items = $this->get('Items');

Here are my complete view sources:

    function display($tpl = null) 
{
    // Get data from the model
    $items = $this->get('Items');
    $pagination = $this->get('Pagination');

    $items2 = $this->get('Items', 'MyComponents');
    $pagination2 = $this->get('Pagination', 'MyComponents');

    // Check for errors.
    if (count($errors = $this->get('Errors'))) 
    {
        JError::raiseError(500, implode('<br />', $errors));
        return false;
    }
    // Assign data to the view
    $this->items = $items;
    $this->pagination = $pagination;
    $this->items2 = $items2;
    $this->pagination2 = $pagination2;

    // Display the template
    parent::display($tpl);
}
DreiBaer
  • 61
  • 1
  • 8