tags:

views:

124

answers:

4

Hello, Im just cracking open cakePHP (v1.3.2). I set it up on my local wamp server, seems to work fine except the delete() function doesn't work. I'm following their blog tutorial exactly, its as simple as it can be so I don't understand why its not working.

Heres the function in my PostsController class:

function delete($id = NULL) {    
    $this->Post->delete($id);    
    $this->Session->setFlash('The post with id: '.$id.' has been deleted.'); 

    $this->redirect(array('action'=>'index')); 
} 

The "Delete" link's url looks like http://localhost/posts/delete/id:1 (where the id number matches the particular post, obviously). It redirects and sets the flash message, however there is no number where $id should be in the message, and the post isn't deleted. It seems the proper id is passed through the url, but I don't think it is getting into the function.

I dont get it. Any ideas???

+1  A: 

I would try http://localhost/posts/delete/1 .... At least that was the URLs "shape" that I remember when I used to work in CakePHP. But that was a while ago...

Cheers, Gianluca.

Gianluca Colucci
+1  A: 

Figured it out, the url was generated improperly. This is a mistake on CakePHP's tutorial.

This is how they suggest you create a link to delete a post:

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

It should be:

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

Notice the difference: the id in the url parameter should not be the key=>value pair 'id' => $post['Post']['id'] but just the value $post['Post']['id'].

Logic Artist
A: 

Gianluca is right, parameter in the function match the passed args from the url. For example:

if you have something like this:

function my_action($param1=null, $param2=null){
    ...
}

If your url is http://localhost/post/myaction/1/2 then in the function $param1=1 and $param2=2 you can pass as many parameters as you want.

links in CakePHP style should be as you pointed out:

<?php echo $html->link('Delete', array('action' => 'my_action', $param1, $param2)...);?>

If you want to access id:1 then you need to get it from

$this->params['named']['id']

from the controller.

HTH

Nik
A: 

Thanks, it worked. I have trouble when I try to delete record, I followed cake PHP tutorial but can not run the script, till I found this article.

firmansyah