Joomla调用另一个模型并填充数据

问题描述 投票:0回答:1

我正在为Joomla 3.x开发Joomla组件。

该组件用于记帐和管理属性。

在属性视图中,我单击了一个按钮,该按钮转到另一个视图,用于上传文件。

我在URL中提供了对象ID,但是我认为这有点不安全,并且也很难通过重定向进行管理。

我找不到正确的做法。我的想法是在属性视图中获取模型并设置属性ID,然后在单击按钮时跳至该视图。

在视图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::_('COM_IMMO_DASHBOARD_PDF') ?></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'] : '' ?>"/>

感谢您的帮助

php model-view-controller joomla3.0 joomla-component
1个回答
0
投票

视图中的按钮将链接到控制器。请注意,您无需在此处设置视图或布局。

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设置为State。

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

© www.soinside.com 2019 - 2024. All rights reserved.