2011-01-29 82 views
5

我有一個Zend表格來添加一些東西到數據庫。然後我想用這個表單來編輯我添加到數據庫中的內容。是否有任何可能使用這種形式(從數據庫填充它,並顯示它???) 我在我的控制器有這樣的:ZEND,編輯表格

public function editAction() { 

    if (Zend_Auth::getInstance()->hasIdentity()) { 
     try { 
      $form = new Application_Form_NewStory(); 
      $request = $this->getRequest(); 
      $story = new Application_Model_DbTable_Story(); 
      $result = $story->find($request->getParam('id')); 

      // $values = array(
      //  'title' => $result->title, 
      //  'story' => $result->story, 
      //); 

      if ($this->getRequest()->isPost()) { 
       if ($form->isValid($request->getPost())) { 
        $data = array(
         'title' => $form->getValue("title"), 
         'story' => $form->getValue("story"), 
        ); 
        $where = array(
         'id' => $request->getParam('id'), 
        ); 
        $story->update($data, $where); 
       } 
      } 
      $this->view->form = $form; 
      $this->view->titleS= $result->title; 
      $this->view->storyS= $result->story; 
     } catch (Exception $e) { 
      echo $e; 
     } 
    } else { 
     $this->_helper->redirector->goToRoute(array(
      'controller' => 'auth', 
      'action' => 'index' 
     )); 
    } 
} 

筆者認爲:

<?php 
try 
{ 
$tmp = $this->form->setAction($this->url()); 
//$tmp->titleS=$this->title; 
//$tmp->storyS=$this->story; 

//echo $tmp->title = "aaaaa"; 
} 
catch(Exception $e) 
{ 
    echo $e; 
} 

當我嘗試改變這個視圖中的東西我的意思是給任何不同的值然後NULL我有錯誤,我不能這樣做,因此是否有任何可能重用這種形式?或不?

謝謝!

回答

11

Zend_Form有方法填充(),它根據數組數據設置表單的值。所以只是做:

$form->populate($result->current()->toArray()); 

和形式將基於陣列中的鍵填充。

+1

是的它的工作原理!非常感謝你! – canimbenim 2011-01-29 23:13:05