简体   繁体   English

从joomla前端保存数据

[英]saving data from joomla frontend

I was looking for a solution as to how to save data from joomla frontend. 我一直在寻找如何从joomla前端保存数据的解决方案。 I came across the following code for controller and model which works perfectly. 我遇到了以下控制器和模型的代码,它完美地运行。 But I was looking for a standard practice like its done in the back end using jform, jtable etc ... In the following code (inside model), the saving technique do not look so appealing. 但我正在寻找一种标准的做法,比如它在后端使用jform,jtable等完成......在下面的代码(内部模型)中,保存技术看起来并不那么吸引人。 And I am totally without any idea how the server side validations is implemented. 我完全不知道服务器端验证是如何实现的。

It might be confusing, so i would like to reiterate that in the backend we don't even have to write the add or save or update function, it is automatically handled by the core classes with both client and server side validation. 这可能会令人困惑,所以我想重申一下,在后端我们甚至不必编写添加或保存或更新功能,它由核心类自动处理,同时进行客户端和服务器端验证。 So i was looking for something like that. 所以我一直在寻找类似的东西。

Controller 调节器

<?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;
    }
}

Model 模型

<?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;
        }
    }
}

Can somebody kindly point me to a good tutorial or share me a component which deals with form in the frontend with joomla 2.5. 有人可以指点我一个很好的教程或分享一个组件,与joomla 2.5在前端处理表单。

use the following code in your model 在模型中使用以下代码

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

You should be able to use jcontrollerform's methods directly, instead of writing your own submit()-method (and updItem()) like you do. 您应该能够直接使用jcontrollerform的方法,而不是像您一样编写自己的submit()方法(和updItem())。 I describe something similar here . 在这里描述类似的东西 This means you display your form the usual way using jform, and use action="index.php?option=com_jobs&task=save&view=registration&id=whateverid" 这意味着您使用jform以常规方式显示表单,并使用action =“index.php?option = com_jobs&task = save&view = registration&id = whateverid”

This way jcontrollerform->save() is used, which in turn calls your model's save(). 这样使用jcontrollerform-> save(),它又调用模型的save()。 (Hmmm, this probably means your model should extend JModelAdmin instead of JModelForm, to include the relevant methods. ) This will run all the necessary validation checks etc. (嗯,这可能意味着你的模型应该扩展JModelAdmin而不是JModelForm,以包含相关的方法。)这将运行所有必要的验证检查等。

You might need to register paths for the model, table and form that you want to be using, like I do in the link. 您可能需要为要使用的模型,表格和表单注册路径,就像我在链接中一样。

You need to include the id in the url parameters if you edit existing data, because the jform[id] - parameter will be ignored. 如果编辑现有数据,则需要在url参数中包含id,因为jform [id] - 参数将被忽略。

Sorry I dont have any good tutorial or whatever for you, hope this helps. 对不起,我没有任何好的教程或其他适合你,希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM