开发者

Deleting database records from cakephp NOT WORKING

开发者 https://www.devze.com 2023-02-25 11:04 出处:网络
Here\'s my code: function admin_delete($id = null){ $booking = $this->Booking->read(null, $id); if($this->Booking->delete($id)) {

Here's my code:

function admin_delete($id = null){
    $booking = $this->Booking->read(null, $id);
    if($this->Booking->delete($id)) {
        $t开发者_Go百科his->Session->setFlash('Booking deleted');
        $this->redirect(array('action'=>'index'));
    }
}

and this doesn't seem to work. I've been fiddling with this for ages trying different code etc, using del() as well, but nothing seems to work. The link on the delete button is perfect (eg.com/bookings/delete/id:23), but it just tries to reach the admin_delete.ctp view. which I've clearly asked it to ignore by the redirect.

What am I doing wrong?

my delete link in the view:

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

HALP!


if you are using admin routing you can not call 'action'=>'admin_delete' directly, its 'action'=>'delete', 'admin' => true


You're using named parameters, which aren't passed to the function as a parameter. If you want to keep using named parameters do the following. In config/routes.php, add:

Router::connectNamed(array('id'));

Rewrite your admin_delete() function to access the id parameter via the named parameters array:

function admin_delete(){
    if(isset($this->params['named']['id']) && $this->Booking->delete($this->params['named']['id'])) {
        $this->Session->setFlash('Booking deleted');
        $this->redirect(array('action'=>'index'));
        exit();
    }
}

Alternatively, if you want to keep it simple and just not use named parameters, you can just update your delete link to not use them. (remove "'id'=>"):

<?php echo $html->link('Delete', array('action'=>'admin_delete', $booking['Booking']['id']), null, 'Are you sure?');?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号