2011-01-29 83 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