2014-08-28 7 views
1

나는 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 프론트 엔드에서 양식을 다루는 구성 요소를 공유 할 수 있습니다.

답변

0

jControllerform의 메소드를 직접 작성하는 대신 submit() - 메소드 (및 updItem())를 직접 작성하는 것이 가능해야합니다. 나는 비슷한 것을 설명한다 here. 이다) (

이 방법 jcontrollerform-> 저장 "&보기 = 등록 & ID = whateverid 저장 = = index.php를? 옵션 & 작업을 com_jobs"당신이 jform를 사용하여 양식 일반적인 방법을 표시하고 행동 =을 사용하여 의미 used는 모델의 save()를 호출합니다. (흠, 아마도 이것은 모델이 JModelForm 대신 JModelForm을 확장하여 관련 메소드를 포함해야 함을 의미합니다.) 그러면 필요한 모든 유효성 검사 등이 실행됩니다.

모델, 테이블 및 양식에 대한 경로를 등록해야 할 수도 있습니다. 내가 링크에서하는 것처럼 사용하고 싶습니다.

jform [id] 매개 변수가 무시되므로 기존 데이터를 편집하는 경우 url 매개 변수에 id를 포함해야합니다.

죄송합니다. 자습서가 없거나 도움이 되었기를 바랍니다.

+0

확인하면 모델에 다음 코드를 사용합니다. 그러나 모델은 상호 작용해야하는 데이터베이스 테이블을 모델이 어떻게 알 수 있습니까? JTable이 여기에 없습니다. – raaman

+0

/관리자에 jtable이있는 경우이 경로를 포함 할 수 있습니다. 그렇지 않다면 jtable 클래스를 작성해야한다고 생각합니다 ... – jonasfh

+0

컨트롤러와 모델이 어떻게 보이는지 저를 공유 할 수 있다면 감사 할 것입니다. @jonasfh 그래도 여전히 상황을 파악할 수 없기 때문에 – raaman

3

양식()가이 컨트롤러 -> 저장 제출 호출 차례로 모델 -> 저장()도 호출

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