0

我有包含以下代码的文件(ProfileController.php):

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(
           //     'names' => $result->names,
           //     'password' => $result->password,
           // );

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $data = array(
                        'names' => $form->getValue("names"),
                        'password' => $form->getValue("password"),
                    );
                    $form->populate($data->toArray());
                    $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'
        ));
    }

    }

和另一个包含以下代码的文件(edit.phtml):

    <?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;
            }

    ?>

我希望用户能够编辑他们的用户名和密码。我该怎么做?

4

1 回答 1

0

首先:将 Zend_Auth 的东西移到 init() 或 preDispatch(),这样 Auth 将针对控制器中的任何或所有操作运行。

使多个提交按钮起作用的技巧是为按钮赋予不同的名称,以便 getParam('') 可以使用。

通常我只在删除时做这种事情,对于编辑或更新我只是将整个数组提交回数据库。我通常使用该Zend_Db_Table_Row save()方法而不是Zend_Db_Table's insert()这样update()的机制有点不同。

我只是使用一个简单的表单来执行更新,这里是控制器代码(视图只是回显的表单):

//update album information
public function updatealbumAction()
    {   //get page number from session
        $session = new Zend_Session_Namespace('page');
        //get album id
        $id = $this->getRequest()->getParam('id');

        $model = new Music_Model_Mapper_Album();
        //fetch the album database record
        $album = $model->findById($id);

        $form = new Admin_Form_Album();
        //this form is used elsewhere so set the form action to this action
        $form->setAction('/admin/music/updatealbum/');

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $data = $form->getValues();//get valid and filtered form values

                $newAlbum = new Music_Model_Album($data);//create new entity object

                $update = $model->saveAlbum($newAlbum);//save/update album info

                $this->message->addMessage("Update of Album '$update->name' complete!");//generate flash message
                $this->getHelper('Redirector')->gotoSimple('update', null, null, array('page' => $session->page));//redirect back to the page the request came from
            }
        } else {
            $form->populate($album->toArray());
            $this->view->form = $form;
        }
    }

这是一个非常常见的更新操作。

现在,您可以使用不同的请求参数对记录执行操作。我用它来删除数据库记录,但一切皆有可能。

public function deleteAction()
    {
        $session = new Zend_Session_Namespace('page');
        $request = $this->getRequest()->getParams();
        try {
            switch ($request) {
                //if 
                case isset($request['trackId']):
                    $id = $request['trackId'];
                    $model = new Music_Model_Mapper_Track();
                    $model->deleteTrack($id);
                    $this->message->addMessage("Track Deleted!");

                    break;
                case isset($request['albumId']):
                    $id = $request['albumId'];
                    $model = new Music_Model_Mapper_Album();
                    $model->deletealbum($id);
                    $this->message->addMessage("Album Deleted!");

                    break;
                case isset($request['artistId']):
                    $id = $request['artistId'];
                    $model = new Music_Model_Mapper_Artist();
                    $model->deleteArtist($id);
                    $this->message->addMessage("Artist Deleted!");

                    break;

                default:
                    break;
            }
            $this->getHelper('Redirector')->gotoSimple('update', null, null, array('page' => $session->page));
        } catch (Exception $e) {
            $this->message->addMessage($e->getMessage());
            $this->getHelper('Redirector')->gotoSimple('update', null, null, array('page' => $session->page));
        }
    }

您可以将请求参数作为提交按钮标签或 url 或任何适合您的方式传递。

祝你好运!

于 2012-11-29T11:08:21.067 回答