从 cakephp 删除数据库记录不起作用

发布于 2024-11-01 06:43:17 字数 662 浏览 2 评论 0原文

这是我的代码:

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() 摆弄这个问题,但似乎没有任何效果。删除按钮上的链接是完美的(例如.com/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!

Here's my code:

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里寻她 2024-11-08 06:43:17

如果您使用管理路由,则无法直接调用 'action'=>'admin_delete',其 'action'=>'delete', 'admin' =>正确

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

2024-11-08 06:43:17

您使用的是命名参数,这些参数不会作为参数传递给函数。如果您想继续使用命名参数,请执行以下操作。在 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'=>”):

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

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?');?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文