保存来自joomla前端的数据


saving data from joomla frontend

我一直在寻找如何从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的前端处理表单。

在模型中使用以下代码

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

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

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

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

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

对不起,我没有任何好的教程或其他什么给你,希望这有帮助。