如何在使用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

查看:137
本文介绍了如何在使用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

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?

推荐答案

通过直接在两个 view 文件中调用正确形成的模型,我能够从同一视图成功使用多个模型.我没有遵循Joomla文档,因为我没有修改任何可能的 controller (一个是整个组件的控制器,另一个是特定于视图的控制器).我也没有使用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/多视图名称/view.html.php(将模型传递给视图)
  • /site/views/多视图名称/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/多视图名称/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/多视图名称/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类的注意,该类是在尝试使用正式记录的方法从Google遗留在服务器上的PHP错误消息PHP Notice: Undefined index:中搜索google时所发现的.

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.

这篇关于如何在使用Component Creator构建的Joomla 3.x组件的一个视图中包含多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆