【问题标题】:saving data from joomla frontend从 joomla 前端保存数据
【发布时间】:2014-08-28 18:53:09
【问题描述】:

我正在寻找如何从 joomla 前端保存数据的解决方案。我遇到了以下完美运行的控制器和模型代码。但我一直在寻找一种标准做法,就像它在后端使用 jform、jtable 等完成的那样……在下面的代码(模型内部)中,保存技术看起来并不那么吸引人。而且我完全不知道服务器端验证是如何实现的。

这可能会令人困惑,所以我想重申一下,在后端我们甚至不必编写添加、保存或更新函数,它由核心类自动处理,并具有客户端和服务器端验证。所以我一直在寻找类似的东西。

控制器

<?php

// No direct access.
defined('_JEXEC') or die;

// Include dependancy of the main controllerform class
jimport('joomla.application.component.controllerform');

class JobsControllerRegistration extends JControllerForm
{
    public function getModel($name = 'Registration', $prefix = 'JobsModel', $config = array('ignore_request' => true))
    {
        return parent::getModel($name, $prefix, array('ignore_request' => false));
    }

    public function submit()
    {
       // Check for request forgeries.
       JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        // Initialise variables.
        $app    = JFactory::getApplication();
        $model  = $this->getModel('Registration');

        // Get the data from the form POST
        $data = JRequest::getVar('jform', array(), 'post', 'array');

        $form   = $model->getForm();
        if (!$form) {
            JError::raiseError(500, $model->getError());
            return false;
        }

        // Now update the loaded data to the database via a function in the model
        $upditem    = $model->updItem($data);

        // check if ok and display appropriate message. This can also have a redirect if desired.
        if ($upditem) {
            echo "<h2>Joining with us is successfully saved.</h2>";
        } else {
            echo "<h2>Joining with us faild.</h2>";
        }

    return true;
    }
}

型号

<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Include dependancy of the main model form
jimport('joomla.application.component.modelform');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
// Include dependancy of the dispatcher
jimport('joomla.event.dispatcher');
/**
* HelloWorld Model
*/
class JobsModelRegistration extends JModelForm
{
    /**
     * @var object item
     */
    protected $item;

    /**
     * Get the data for a new qualification
     */
    public function getForm($data = array(), $loadData = true)
    {

        $app = JFactory::getApplication('site');

        // Get the form.
        $form = $this->loadForm('com_jobs.registration', 'registration', array('control' => 'jform', 'load_data' => true),true);

        if (empty($form)) {
            return false;
        }
        return $form;
    }

    //Nwely added method for saving data
    public function updItem($data)
    {
        // set the variables from the passed data
        $fname = $data['fname'];
        $lname = $data['lname'];
        $age = $data['age'];
        $city = $data['city'];
        $telephone = $data['telephone'];
        $email = $data['email'];
        $comments = $data['comments'];

        // set the data into a query to update the record
        $db = $this->getDbo();
        $query  = $db->getQuery(true);
        $query->clear();

        $db =& JFactory::getDBO();
        $query = "INSERT INTO #__joinwithus ( `id`, `firstname`, `lastname`, `age`, `city`, `telephone`, `email`, `comment`)
    VALUES (NULL,'" . $fname . "','" . $lname . "','" . $age . "','" . $city . "','" . $email . "','" . $telephone . "','" . $comments . "')";

        $db->setQuery((string)$query);

        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            return true;
        }
    }
}

谁能给我指点一个好的教程或分享一个使用 joomla 2.5 在前端处理表单的组件。

【问题讨论】:

    标签: php joomla joomla2.5


    【解决方案1】:

    在您的模型中使用以下代码

    $data = $app->input->getArray($_POST);
    $query  = $db->getQuery(true);
    

    【讨论】:

      【解决方案2】:

      您应该能够直接使用 jcontrollerform 的方法,而不是像您那样编写自己的 submit() 方法(和 updItem())。我描述了类似here 的东西。这意味着您使用 jform 以通常的方式显示表单,并使用 action="index.php?option=com_jobs&task=save&view=registration&id=whateverid"

      这种方式使用 jcontrollerform->save(),然后调用模型的 save()。 (嗯,这可能意味着您的模型应该扩展 JModelAdmin 而不是 JModelForm,以包含相关方法。)这将运行所有必要的验证检查等。

      您可能需要为要使用的模型、表格和表单注册路径,就像我在链接中所做的那样。

      如果您编辑现有数据,则需要在 url 参数中包含 id,因为 jform[id] - 参数将被忽略。

      对不起,我没有任何好的教程或任何东西给你,希望这会有所帮助。

      【讨论】:

      • 好的,一旦表单被提交,controller->save() 就会被调用,然后 model->save() 也会被调用。但是模型如何知道它应该与哪个数据库表交互呢?这里缺少 JTable 的东西
      • 如果你在 /administrator 中有一个 jtable,你可以包含它的路径。如果不是我猜你必须写一个 jtable 类...
      • 如果你能分享一下你的控制器和模型的样子,那将不胜感激@jonasfh 因为我仍然无法弄清楚
      • 嗨,如果你有一个好的解决方案,我也需要它。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多