从 cakephp 删除数据库记录不起作用
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用管理路由,则无法直接调用
'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
您使用的是命名参数,这些参数不会作为参数传递给函数。如果您想继续使用命名参数,请执行以下操作。在 config/routes.php 中,添加:
重写您的 admin_delete() 函数以通过命名参数数组访问 id 参数:
或者,如果您想保持简单并且不使用命名参数,您可以将删除链接更新为不使用它们。 (删除“'id'=>”):
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:
Rewrite your admin_delete() function to access the id parameter via the named parameters array:
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'=>"):