如何在使用 Component Creator 构建的 Joomla 3.x 组件的一个视图中包含多个模型

How can I include multiple models in one view for in a Joomla 3.x component built with Component Creator

Joomla 组件使用 MVC 模型。 Component Creator 是一种广泛使用的工具,其付费级别支持使用 SQL 导入创建多表视图。此外,开发人员根据 Joomla 文档从头开始构建组件。

我想构建一个高级组件,作为 "dashboard" 显示来自多个数据库表的数据,具有所有管理员后端和访问者前端 CRUD(创建、读取、更新、删除)功能Joomla 的。这意味着我需要从屏幕上显示的多个数据库表中绘制多个模型(来自 MVC 哲学)同时

Joomla Documentation 建议将以下代码插入 "controller task-method" 以提供信息:

$view = $this->getView( 'model-a', 'html' );
$view->setModel( $this->getModel( 'model-a' ), true );
$view->setModel( $this->getModel( 'model-b' ) );
$view->display();

然后在视图显示方法中调用这些模型:

$item1 = $this->get( 'data1' );
$item2 = $this->get( 'data2', 'model-b' );

但是,Joomla 文档中提供的这些说明不充分或与按照提供的 Joomla Hello World Tutorial 教程构建的组件或从广泛使用和流行的组件创建器工具构建的组件不兼容。要么组件在调用时无法加载页面,要么不会通过简单的复制将数据传递到视图并粘贴到组件创建者或 Joomla hello world 教程创建的多个控制器中的任何一个。

如何在 Joomla 3.X 组件的同一视图中调用多个模型?

通过直接在两个 view 文件中调用正确形成的模型,我能够从同一视图成功使用多个模型。我没有遵循 Joomla 文档,因为我没有修改任何可能的 controller(一个是整个组件的控制器,另一个是特定于视图的控制器)。我也没有使用 Joomla 文档中提供的功能,因为它们会产生错误。

根据正确的 Joomla MVC 约定,视图由相关视图目录和子文件夹中的两个文件创建:

  • /site/views/multiviewname/view.html.php(将模型传递给视图)
  • /site/views/multiviewname/tmpl/default.php(具有 HTML 模板)

这两个都需要更改才能同时查看多个模型的数据。假设您的所有其他视图、控制器和模型都已正确构建,这是有效的,就像使用 'Component Creator' 工具时自动完成的那样。我的组件有数百个文件,包括 css、后端管理、安装、语言等。所有这些都是使用组件创建工具即时构建的。

经过删减但仍然完整的功能代码如下:

/site/views/多视图名称/view.html.php

<?php

jimport('joomla.application.component.view');

class ComponentnameViewMultiviewname extends JViewLegacy
{
//  $items is for the default model
    protected $items;
//  $ItemsOtherModel is for second model. Notice the '$' used here but not elsewhere   
    protected $ItemsOtherModel;

    public function display($tpl = null)
    {
        $app = JFactory::getApplication();
        $this->state = $this->get('State');
        $this->items = $this->get('Items');
        $this->pagination = $this->get('Pagination');
        $this->params     = $app->getParams('com_componentname');

//  sets default model
        $this->setModel( $this->getModel( 'model-a' ), true );
//  sets second model & uses 'JModelLegacy,' contrary to documentation
        $this->setModel(JModelLegacy::getInstance('model-b', 'componentnameModel'));
//  assigns array from the second model to 'ItemsOtherModel.' there is no '$' sign used.
        $this->ItemsOtherModel = $this->get('Items','model-b');

        parent::display($tpl);
    }

}

/site/views/multiviewname/tmpl/default.php

<?php

echo "<h3>Items from default model</h3> ";
echo var_dump($this->items);

echo "<h3>items from secondary model</h3> ";
//  notice that the '$' is absent from 'ItemsOtherModel'
echo var_dump($this->ItemsOtherModel);

只有经过几天的研究才有可能实现这一突破。已付费 Component Creator tool was invaluable to start me off with well formed code that adheres to Joomla MVC component standards. After working with and examining all the files for days, the I found the prompt I needed in this google groups thread,提醒我注意 JModelLegacy class,在搜索 google 时发现来自 PHP 错误消息 PHP Notice: Undefined index: 的术语我的服务器在尝试使用官方记录的方法时。

在浏览器中呈现的这个页面只是将数据库中的所有信息 table 转储到该页面,但进一步的开发可以创建我最终需要的格式化和功能仪表板。

此代码用于显示信息列表,而不是多个单个项目。将多个模型添加到一个视图的 Joomla 文档是为多个单个项目设置的,而不是此处显示的项目数组。