Zend FrameWork Part-12


 

Zend FrameWork Part-12

This is the last installment of the ZF tutorial and in this page we will study how to delete any record. In next tutorial we will study about few basic and advance features of ZF.

This is the last installment of the ZF tutorial and in this page we will study how to delete any record. In next tutorial we will study about few basic and advance features of ZF.

Zend Framework-12:

Delete Book:

To complete the application we have to add the deletion part as well. We will put a delete link after each entry (record) in our list page, this link will take us to other page which will ask for our confirmation. If we click the yes button then the record will be deleted from the record and if we click the no button then this will take us to the index page.

Like the add and edit function in delete function we will use Request's isPost() method. This method will determine whether we will display the confirmation page or not. We use the Application_Model_DbTable_Books model to delete the record from the table using deleteBook() method.

 

.../application/form/Book.php:

...

public function deleteAction()

{

$this->view->title = "Delete Book";

$this->view->headTitle($this->view->title);

if ($this->getRequest()->isPost()) {

$del = $this->getRequest()->getPost('del');

if ($del == 'Yes') {

$id = $this->getRequest()->getPost('id');

$books = new Application_Model_DbTable_Books();

$books->deleteBook($id);

}

$this->_helper->redirector('index');

}

else {

$id = $this->_getParam('id', 0);

$books = new Application_Model_DbTable_Books();

$this->view->book = $books->getBook($id);

}

}

...

../application/views/scripts/index/add.phtml

<p>Are you sure that you want to delete

0

'<?php echo $this->escape($this->book['title']); ?>' by

'<?php echo $this->escape($this->book['author']); ?>'?

</p>

1

<form action="<?php echo $this->url(array('action'=>'delete')); ?>" method="post">

<div>

<input type="hidden" name="id" value="<?php echo $this->book['id']; ?>" />

2

<input type="submit" name="del" value="Yes" />

<input type="submit" name="del" value="No" />

</div>

3

</form>

 

Output:

4

ZF Delete

Ads