2012-01-10 4 views
2

私は私のコントローラ(管理者)にこのコードを持っている:私が持っているモデルでは編集フォームに戻るには?

function save(){ 
     $model = $this->getModel('mymodel'); 

     if ($model->store($post)) { 
      $msg = JText::_('Yes!'); 
     } else { 
      $msg = JText::_('Error :('); 
     } 
     $link = 'index.php?option=com_mycomponent&view=myview'; 
     $this->setRedirect($link, $msg); 
} 

function store(){ 
     $row =& $this->getTable(); 

     $data = JRequest::get('post'); 
     if(strlen($data['fl'])!=0){ 
      return false; 
     } 

     [...] 

そして、これは働いている - エラーメッセージを生成し、それは項目のリストビューに戻ります。入力したデータを編集ビューに表示したいどうやってするの?通常、このメソッド「loadFormData」の中にロードされている

JFactory::getApplication()->getUserState('com_mycomponent.edit.mymodel.data', array()); 

:その後、

if ($model->store($post)) { 
    $msg = JText::_('Yes!'); 
} else { 
    // stores the data in your session 
    $app->setUserState('com_mycomponent.edit.mymodel.data', $validData); 

    // Redirect to the edit view 
    $msg = JText::_('Error :('); 
    $this->setError('Save failed', $model->getError())); 
    $this->setMessage($this->getError(), 'error'); 
    $this->setRedirect(JRoute::_('index.php?option=com_mycomponent&view=myview&id=XX'), false)); 
} 

は、あなたのようなものとのセッションからのデータをロードする必要がありますすることができますあなたのコントローラで

答えて

5

あなたのモデル。そのデータをロードする場所は、コンポーネントの実装方法によって異なります。 Joomlaのフォームapiを使用している場合は、次のメソッドをモデルに追加できます。

protected function loadFormData() 
{ 
    // Check the session for previously entered form data. 
    $data = JFactory::getApplication()->getUserState('com_mycomponent.edit.mymodel.data', array()); 

    if (empty($data)) { 
     $data = $this->getItem(); 
    } 

    return $data; 
} 

EDIT:

しかし注意してください、あなたのコントローラは、「JControllerForm」から継承する場合のJoomlaのAPIは、すでにあなたのためのすべてのこれを行うことができますことを、あなたは、保存方法を書き直す必要はありません。あなたのコンポーネントを作成する最良の方法は、Joomlaのコアコンポーネントであるcom_contentにあるものをコピーすることです。

0

saveなどの書き換えはお勧めしません。

実際に何かを無効にしたい場合、保存の前後に何かを更新したい場合は、JTableファイルを使用する必要があります。例については

/** 
* Example table 
*/ 
class HelloworldTableExample extends JTable 
{ 
    /** 
    * Method to store a node in the database table. 
    * 
    * @param boolean $updateNulls True to update fields even if they are null. 
    * 
    * @return boolean True on success. 
    */ 
    public function store($updateNulls = false) 
    { 
     // This change is before save 
     $this->name = str_replace(' ', '_', $this->name); 

     if (!parent::store($updateNulls)) 
     { 
      return false; 
     } 

     // This function will be called after saving table 
     AnotherClass::functionIsCallingAfterSaving(); 
    } 
} 

あなたはJTableのクラスを使用して任意の方法を拡張することができますし、それはそれをやってする推奨方法です。

関連する問題