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

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

Joomla components use the MVC model. Component Creator is a widely used tool whose paid level supports creation of multi tabled views with SQL import. Also, developers build components from scratch according to Joomla documentation.

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

I want to build an advanced component that functions as a "dashboard" displaying data from multiple database tables with all the administrator back-end and visitor front-end CRUD (CREATE, READ, UPDATE, DELETE) capabilities of Joomla. This means that I need multiple models (from the MVC philosophy) drawing from multiple database tables shown on the screen simultaneously.

Joomla 文档建议插入以下代码进入控制器任务方法"以提供信息:

Joomla Documentation suggests the following code be inserted into the "controller task-method" to make the information available:

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

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

and then later call upon those models with the in the views display method:

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

然而,Joomla 文档中提供的这些说明与遵循提供的 Joomla Hello World 教程 教程或从广泛使用和流行的组件创建器工具构建的组件.组件在调用时无法加载页面,或者不会通过简单的复制和粘贴将数据传递到视图,并粘贴到由组件创建者或 Joomla hello world 教程创建的多个控制器中的任何一个.

However, these instructions provided in Joomla documentation are insufficient or incompatible with the component built when following the provided Joomla Hello World Tutorial tutorial or components built from the widely used and popular Component Creator tool. Either the component will fail to load the page when called upon or will not pass the data to the view with a simple copy and paste into any one of the multiple controller created by component creator or Joomla hello world tutorial.

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

How can I call upon multiple models in the same view for a Joomla 3.X component?

推荐答案

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

I was able to successfully use multiple models from the same view by making calls directly in the two view files to properly formed models. I did not follow Joomla documentation because I didn't modify either possible controller (one being the controller for the entire component and the other controller being view-specific). I also did not use the functions provided in Joomla documentation, as those produced errors.

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

According to proper Joomla MVC convention, a view is created by two files in the relevant view directory and subfolder:

  • /site/views/multiviewname/view.html.php(将模型传递给视图)
  • /site/views/multiviewname/tmpl/default.php(包含 HTML 模板)
  • /site/views/multiviewname/view.html.php (which passes the model to the view)
  • /site/views/multiviewname/tmpl/default.php (which has the HTML template)

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

Both these need to be changed to view data from more than one model at the same time. This works assuming that all of your other views, controllers, and models are built properly, as is done automatically when using the 'Component Creator' tool. My component had hundreds of files, including css, backend administration, installation, language, etc. All of these were build in moments with the component creator tool.

经过删节但仍然完整的代码如下:

The abridged but still completely functional code is as follows:

/site/views/multiviewname/view.html.php

/site/views/multiviewname/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

/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 工具非常宝贵,它让我开始使用格式良好的代码遵守 Joomla MVC 组件标准.在使用并检查所有文件几天后,我在 这个 google 组线程,让我注意到 JModelLegacy 类,在从 PHP 错误消息 PHP 通知:未定义索引:code> 在尝试使用官方记录的方法时留在我的服务器上.

This breakthrough was only possible after days of research. The paid 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, calling my attention to the JModelLegacy class, found when searching google for terms from the PHP error message PHP Notice: Undefined index: left on my server when attempting to use the officially documented methods.

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

This page rendered in the browser simply dumps out all information from the database table to the page, but further development can create the formatted and functional dashboard that I will ultimately need.

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

This code is for displaying lists of information, as opposed to multiple single items. The Joomla documentation for adding multiple models to one view is set up for multiple single items, rather than arrays of items shown here.

相关文章