我正在尋找一個解決方案來保存joomla前端的數據。我遇到了以下代碼,用於完美控制器和模型。但我正在尋找一種標準的做法,就像使用jform,jtable等在後端完成的一樣......在下面的代碼中(模型內部),保存技術看起來不那麼吸引人。我完全不知道服務器端驗證是如何實現的。從joomla前端保存數據
這可能會讓人困惑,所以我想重申,在後端我們甚至不必編寫添加或保存或更新函數,它將由核心類自動由客戶端和服務器端驗證。所以我在尋找那樣的東西。
控制器
<?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的組件。
一次OK的形式提交的控制器 - >保存()被調用,進而模型 - >保存()也被調用。但是模型如何知道它應該與哪個數據庫表進行交互? JTable的東西在這裏丟失 – raaman 2014-08-29 00:45:46
如果你在/管理員中有一個jtable,你可以包含這個路徑。如果不是,我猜你必須寫一個jtable類... – jonasfh 2014-08-29 04:44:15
如果你能分享我的控制器和模型如何,那麼將不勝感激@jonasfh因爲我仍然無法弄清楚事情 – raaman 2014-08-29 09:42:34