【问题标题】:Joomla 2.5 loading different models in view [duplicate]Joomla 2.5在视图中加载不同的模型[重复]
【发布时间】:2013-01-31 09:23:53
【问题描述】:

可能重复:
How to use multiple models in joomla MVC Component

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

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

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

最好的问候 比约恩

【问题讨论】:

  • 不知道为什么会翻倍。请给我可能重复的问题的链接。

标签: joomla2.5 joomla-extensions


【解决方案1】:

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

然后在视图中用

调用它
    $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-04-07
    • 2014-08-02
    • 2021-06-21
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 2012-07-21
    相关资源
    最近更新 更多