Updating the Bug Record

Now you need to create a method in the Bug model to update an existing record. It will be virtually identical to the createBug() method except that you are going to pass the method an ID (the primary key), and it is going to find the row to update rather than creating a new one (see Listing 4-30).

Listing4-30. The updateBug() Method in application/models/Bug.php public function updateBug($id, $name, $email, $date, $url, $description, $priority, $status)

// find the row that matches the id $row = $this->find($id)->current();

$row->description = $description;

$row->priority = $priority;

// save the updated row $row->save(); return true; } else {

throw new Zend_Exception("Update function failed; could not find row!");

With this method in place, you simply have to update the editAction() method in the bug controller to call this method on a postback (see Listing 4-31). This will function identically to the submitAction() method.

Listing4-31. The UpdatededitAction() Method in application/controllers/BugController.php public function editAction() {

$bugModel = new Model_Bug(); $bugReportForm = new Form_BugReportForm(); $bugReportForm->setAction('/bug/edit'); $bugReportForm->setMethod('post'); if($this->getRequest()->isPost()) {

if($bugReportForm->isValid($_POST)) { $bugModel = new Model_Bug(); // if the form is valid then update the bug $result = $bugModel->updateBug( $bugReportForm->getValue('id'), $bugReportForm->getValue('author'), $bugReportForm->getValue('email'), $bugReportForm->getValue('date'), $bugReportForm->getValue('url'), $bugReportForm->getValue('description'), $bugReportForm->getValue('priority'), $bugReportForm->getValue('status')

$id = $this->_request->getParam('id'); $bug = $bugModel->find($id)->current(); $bugReportForm->populate($bug->toArray()); //format the date field

$bugReportForm->getElement('date')->setValue(date('m-d-Y', $bug->date));

$this->view->form = $bugReportForm;

0 0

Post a comment

  • Receive news updates via email from this site