Thursday, November 12, 2009

CakePHP - Delete a Record from Database

If you are following me, you must have learnt how to add a new record to a table.
Let us learn how to delete record.
Steps required:
1. Open file posts_controller.php (Found under '/app/controllers' folder)
2. Add following lines (This is the function to delete record).

function delete($id) {
$this->Post->delete($id);
$this->Session->setFlash('The post with id: '.$id.' has been deleted.');
$this->redirect(array('action'=>'index'));
}
As you can see, $this->Post->delete($id) statement tells Cake (short-form of CakePHP) to delete the record with id = '$id' from posts table.

This is all we need to do in the PostsController (posts_controller.php).

Now let's edit index.ctp file under '/app/views/posts' folder to show 'Delete' option.
Step:
Open the file '/app/views/posts/index.ctp'
Copy the following code and paste it:


<h1>Blog posts</h1>
  <table>
  <tr>
  <th>Id</th>
  <th>Title</th>
  <th>Actions</th>
  <th>Created</th>
  </tr>
 <!-- Here is where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
  <tr>
  <td><?php echo $post['Post']['id']; ?></td>
  <td>
  <?php echo $html->link($post['Post']['title'], 
  "/posts/view/".$post['Post']['id']); ?>
  </td>
  <td>
  <?php echo $html->link('Delete', array('action' => 'delete', 'id' => $post['Post']['id']), null, 'Are you sure?' )?>
  </td>
  <td><?php echo $post['Post']['created']; ?></td>
  </tr>
  <?php endforeach; ?>
</table>

..............................................................
Save the file. 
Now point your browser to:
http://caketest.local/posts
You can see the screen-shot with DELETE option:


If you follow 'Delete' link, Cake will display a message - 'Are you sure?'
If you confirm delete, the selected record gets deleted.

Now, take a close look at the use of $html->link(); function.

We have learnt already that this function creates a link . This $html->link() function is very versatile like most other functions of CakePHP. 
You can create a link simply by specifying 
$html->link('Link Text','Link Url', array('class'=>'link-class-name'), 'Confirmation Msg [Optional]' );

Here we have created a link in a more sophisticated way. Instead of using link url directly, we have used Cake's powerful link generation feature.
We have used an array() instead of a direct link url.
array('action' => 'delete', 'id' => $post['Post']['id']
Note: we have used 'action'=>'delete' and 'id'=>$post['Post']['id'];
Now Cake will create a link like this one on the fly:
http://caketest.local/posts/delete/2

While going through the CakePHP book I have also noticed that in that array() part, you can actually specify the Controllers also.
array('controller'=>'posts','action' => 'delete', 'id' => $post['Post']['id']) 
Cake will create the same link - http://caketest.local/posts/delete/2 -  without getting confused.
But by default Cake can assume the name of the Controllers in question. So, you may not specify it separately.
That's it.

In my next post, I'll show how to edit record with Cake.

2 comments:

Unknown said...

While using the above code to delete, it gave me an error. I searched and found a way. The way is to make a few changes to the code above, in these files:

1. In posts_controller.php file,

Use
function delete($id=Null)

Instead of
function delete()

2. In index.ctp file,

Use
$html->link('Delete', array('action' => 'delete', $post['Post']['id']), null, 'Are you sure?' )

Instead of
$html->link('Delete', array('action' => 'delete', 'id' => $post['Post']['id']), null, 'Are you sure?' )

Thanks to Siddhartha for providing an easy documentation, for Cake Newbies.

Siddhartha said...

Thanks. It is my pleasure that you have found this blog useful.. Yup! THE points you have noted is correct. I made this entry as I was learning cake for self help. Now I see a lot of New-to-cake developers have found this useful. Keep visiting. I'll try to keep the content updated as and when needed.