通过直接在两个视图中进行调用,我能够成功地从同一视图使用多个模型view文件到正确形成的模型。我没有遵循 Joomla 文档,因为我没有修改任何可能的内容控制器(一个是整个组件的控制器,另一个是特定于视图的控制器)。我也没有使用 Joomla 文档中提供的函数,因为这些函数会产生错误。
根据正确的 Joomla MVC 约定,视图是由相关视图目录和子文件夹中的两个文件创建的:
- /网站/浏览量/多视图名称/view.html.php(将模型传递给视图)
- /网站/浏览量/多视图名称/tmpl/default.php(其中包含 HTML 模板)
需要更改这两项才能同时查看多个模型的数据。假设所有其他视图、控制器和模型都已正确构建,这将有效,就像使用“组件创建器”工具时自动完成的那样。我的组件有数百个文件,包括 CSS、后端管理、安装、语言等。所有这些都是使用组件创建工具瞬间构建的。
经过删节但仍然功能完整的代码如下:
/网站/浏览量/多视图名称/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);
}
}
/网站/浏览量/多视图名称/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);
这一突破只有经过数天的研究才有可能实现。付费的组件创建器 http://www.component-creator.com该工具对于我开始编写符合 Joomla MVC 组件标准的格式良好的代码非常有用。经过几天的处理和检查所有文件后,我找到了我需要的提示这个谷歌群组主题 https://groups.google.com/forum/#!topic/joomla-dev-general/9jYXvVJD3ks,让我注意JModelLegacy
类,在 google 搜索 PHP 错误消息中的术语时发现的PHP Notice: Undefined index:
尝试使用官方记录的方法时留在我的服务器上。
在浏览器中呈现的页面只是将数据库表中的所有信息转储到页面,但进一步的开发可以创建我最终需要的格式化且功能齐全的仪表板。
此代码用于显示信息列表,而不是多个单个项目。用于将多个模型添加到一个视图的 Joomla 文档是针对多个单个项目(而不是此处显示的项目数组)设置的。