php - Joomla 调用另一个模型并填充数据
问题描述
我正在为 Joomla 3.x 开发一个 Joomla 组件。
该组件用于记帐和管理属性。
在属性视图中,我制作了一个按钮,它可以转到另一个视图,用于上传文件。
我已经在 URL 中给出了对象 ID,但我认为这有点不安全,而且重定向也更难管理。
我找不到正确的方法。我的想法是在属性视图中获取模型并设置属性 id,然后在单击按钮时跳转到该视图。
在 View iv'e 中添加了这一行,但这是错误的,因为如果发生错误,它不会存储属性的 id,而是存储所有其他数据。
$jinput = JFactory::getApplication()->input;
if (empty($this->item->object_id))
{
$this->item->object_id = $jinput->get('object_id', '');
$this->form->setValue('object_id', null, $this->item->object_id);
}
我想我应该在模型中插入这些数据,但我不知道在哪里以及如何。
谁能给我一个提示如何以正确的方式做到这一点?
还有一种简单的方法可以重定向回被调用的视图吗?到目前为止,我已经这样做了:
查看房产详情:
<div class="action-bt">
<?php $upload_pdf = JRoute::_('index.php?option=com_immo&back=object_detail&view=pdfupload&layout=edit&object_id=' . (int) $this->object->id); ?>
<a href="<?php echo $upload_pdf; ?>"><?php echo JText::_('PDF Upload') ?></a>
</div>
上传控制器
$return = parent::cancel($key);
$back = $this->input->get('jform', array(), 'array');
if (!empty($this->input->get('back')))
{
$url
= \JRoute::_('index.php?option=com_immo&view=object&layout=detail&task=object.detail&id=' . $back['object_id'], false);
// Redirect back to the edit screen.
$this->setRedirect(
$url
);
}
return $return;
编辑 tmpl
<input type="hidden" name="back" value="<?php echo isset($_GET['back']) ? $_GET['back'] : '' ?>"/>
谢谢你的帮助
编辑
我有 2 个视图: 1. 属性视图,它重定向到 PDFUpload(编辑)页面以添加新的 PDF 2. PDF 列表视图,当单击新的时也会打开 PDFUpload(编辑)视图
userState 是一个很好的提示。
但是现在我的问题是如何正确重定向到调用视图我认为这也可以通过 userState 来完成
但我不确定是否有适当的方法来做到这一点。在取消功能上,我看到有一个代码行:
// Check if there is a return value
$return = $this->input->get('return', null, 'base64');
if (!is_null($return) && \JUri::isInternal(base64_decode($return)))
{
$url = base64_decode($return);
}
解决方案
视图中的按钮将链接到控制器。请注意,您没有在此处设置视图或布局。
use \Joomla\CMS\Router\Route;
<a class="btn btn-primary" href="<?php echo Route::_('index.php?option=com_immo&task=object.edit&id=' . $back['object_id'], false); ?>">Edit Object</a>
在您的控制器功能中,您可以将当前正在编辑的 id 设置为状态。
use \Joomla\CMS\Factory;
use \Joomla\CMS\Router\Route;
public function edit($key = NULL, $urlVar = NULL)
{
$app = Factory::getApplication();
// Get the previous edit id (if any) and the current edit id.
$previousId = (int) $app->getUserState('com_immo.edit.object.id');
$editId = $app->input->getInt('id', 0);
// Set the user id for the user to edit in the session.
$app->setUserState('com_immo.edit.object.id', $editId);
// Get the model.
$model = $this->getModel('ObjectForm', 'ImmoModel');
// Check out the item
if ($editId)
{
$model->checkout($editId);
}
// Check in the previous user.
if ($previousId)
{
$model->checkin($previousId);
}
// Redirect to the edit screen.
$this->setRedirect(Route::_('index.php?option=com_immo&view=object&layout=edit', false));
}
然后,该任务会将用户重定向到编辑页面。您可以通过以下方式访问 id:
$editId = $app->getUserState('com_immo.edit.object.id');
您可以在 Joomla API 页面 https://api.joomla.org/cms-3/classes/JApplication.html#method_getUserState阅读更多内容
推荐阅读
- html - 标题上的 CSS ......有些被分解和穿插
- python - 过滤 df 使组之间的值相等 - 熊猫
- mysql - 使用表锁定来防止多个用户在给定时间更新
- multithreading - 多线程代码上的 Kubernetes 工作负载扩展
- elisp - 错误的类型参数:stringp, nil with org-map-entries
- javascript - 在 reactjs 组件中导入普通的 javascript 文件但事件不起作用
- python - 在 Tensorflow 中实现一个展平层
- reactjs - 将数据发布到 Web 应用程序时被 CORS 策略阻止的 XMLHttpRequest
- mysql - 查找 MySQL localhost URL 和/或访问 phpmyadmin
- python - 如何在某些后缀的s3中从一个存储桶复制到另一个存储桶