0

可能重复:
如何在 joomla MVC 组件中使用多个模型

J!2.5 教程中,视图中仅加载了一个模型(默认模型)。J!1.5 中的片段不再与本教程中的技术一起使用。这是我的扩展资料。 控制器.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);
}

这是我的观点。 意见/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);
}

但不幸的是,没有加载model2。这里发生了什么?我的错误在控制器中吗?是否在控制器中正确设置了两个模型?那么我在视图中的代码呢?我是否必须以这种方式加载模型(从 J!1.5 已知)?

最好的问候比约恩

4

1 回答 1

0

我知道了。解决方案是在控制器中注册您的模型(就像我已经做过的那样)。

然后在视图中调用它

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

代替

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

以下是我的完整视图来源:

    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);
}
于 2013-01-31T13:18:44.953 回答