我有一个操作,从用户获取id
并删除与之相关的项目;请求是这样的:
/经理/delete_item/5但是出现错误:
错误:请求的地址/managers/delete_item/5在此服务器上找不到
这是控制器中的delete_item
:
function delete_item($id = null)
{
$this->item->id = $id;
$this->item->status = 0;
if ($this->data) {
if ($this->item->save($this->data)) {
$this->Session->setFlash('Removed', 'default', array('class' => 'success-msg'));
$this->redirect(array('controller' => 'managers', 'action' => 'discount'));
} else {
$this->Session->setFlash('ERR', 'default', array('class' => 'error-msg'));
}
} else {
$this->data = $this->item->read();
}
}
然而,我有一些类似的动作在我的控制器做这样的事情删除,编辑…而不发生错误。
请帮。
看来你正在寻找这个动作的saveField()
方法。这只是更新模型中的一个字段。这应该能奏效:
function delete_item($id = null) {
$this->Item->id = $id;
// Try to update the status
if ($this->Item->saveField('status', 0)) {
$this->Session->setFlash('Removed', 'default', array('class' => 'success-msg'));
} else {
$this->Session->setFlash('ERR', 'default', array('class' => 'error-msg'));
}
// Always redirect, regardless of the outcome (to make sure no view is needed)
$this->redirect(array('controller' => 'managers', 'action' => 'discount'));
}