ZEND,编辑表格

can*_*nim 5 zend-framework zend-form

我有一个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'
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

在我看来:

<?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;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在这个视图中改变某些东西时,我的意思是给任何不同的值,然后我有错误,我不能这样做,所以有可能重用这个形式吗?或不?

谢谢!

Rad*_*kel 11

Zend_Form有方法populate(),它根据数组数据设置表单的值.所以这样做:

$form->populate($result->current()->toArray());
Run Code Online (Sandbox Code Playgroud)

和表单将根据数组中的键填充.