从cakephp删除数据库记录不工作

时间:2011-04-15 02:50:06

标签: database cakephp record

这是我的代码:

function admin_delete($id = null){
    $booking = $this->Booking->read(null, $id);
    if($this->Booking->delete($id)) {
        $this->Session->setFlash('Booking deleted');
        $this->redirect(array('action'=>'index'));
    }
}

这似乎不起作用。我一直在尝试不同的代码等,使用del(),但似乎没有什么工作。删除按钮上的链接是完美的(例如:/bookings/delete/id:23),但它只是尝试访问admin_delete.ctp视图。我明确要求它通过重定向忽略。

我做错了什么?

我在视图中的删除链接:

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

HALP!

2 个答案:

答案 0 :(得分:1)

您正在使用命名参数,这些参数不会作为参数传递给函数。如果要继续使用命名参数,请执行以下操作。在config / routes.php中,添加:

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

重写你的admin_delete()函数,通过命名参数数组访问id参数:

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();
    }
}

或者,如果您想保持简单并且不使用命名参数,则只需更新删除链接即可使用它们。 (删除“'id'=&gt;”):

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

答案 1 :(得分:1)

如果您使用管理员路由,则无法直接致电'action'=>'admin_delete''action'=>'delete', 'admin' => true

相关问题