나는 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 프론트 엔드에서 양식을 다루는 구성 요소를 공유 할 수 있습니다.
확인하면 모델에 다음 코드를 사용합니다. 그러나 모델은 상호 작용해야하는 데이터베이스 테이블을 모델이 어떻게 알 수 있습니까? JTable이 여기에 없습니다. – raaman
/관리자에 jtable이있는 경우이 경로를 포함 할 수 있습니다. 그렇지 않다면 jtable 클래스를 작성해야한다고 생각합니다 ... – jonasfh
컨트롤러와 모델이 어떻게 보이는지 저를 공유 할 수 있다면 감사 할 것입니다. @jonasfh 그래도 여전히 상황을 파악할 수 없기 때문에 – raaman