0

I'm 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 don't get it. Any ideas???

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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'].

1 Comment

im also going through this example. When I press delete it works and I get a message saying it has deleted but it hasn't actually deleted from the database. Can you remember if this is the way it's supposed to happen? Thanks
0

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

Comments

0

Do you have override function beforeDelete() in the model? If yes, this function must be return true;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.