无法从CakePHP 2.4+中的beforeSave方法删除行 [英] Can't delete row from beforeSave method in CakePHP 2.4+

查看:83
本文介绍了无法从CakePHP 2.4+中的beforeSave方法删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在较新版本的Cake中,我无法在我的beforeSave()方法中删除,但它可与早期版本的lib(例如2.2版)一起使用

I cant delete in my beforeSave() method in newer version of Cake but it works with earler libs (e.g. version 2.2)

有人知道如何使其工作吗?

Does anyone know how get it working again without altering the Cake libs?

代码:

public function beforeSave($options = array()) {

if(!empty($this->data['Attachment']['delete']) &&  (int) $this->data['Attachment']['delete'] === 1) {
        if($this->deleteFromDb((int) $this->data['Attachment']['id'])) {
            $this->data['Attachment'] = array();
            return true;
        } else {
            return false;
        }
    }
    return true;
}



public function deleteFromDb($id) {
    if ($this->delete($id)) {
        return true;
    } else {
        return false;
    }
}

以下行返回false,但我不明白原因:

The following line returns false but I don't understand why:

if($this->deleteFromDb((int) $this->data['Attachment']['id']))

如果我将其替换为以下内容,则仍返回false:

If I replace it with the following it is still returns false:

if($this->delete((int) $this->data['Attachment']['id']))

如果我从控制器访问该方法,则返回true,例如

If I access the method from a controller it returns true, e.g.

$this->Model->deleteFromDb($id);

任何帮助都是很好的。

推荐答案

我解决了这个问题,在较新的Cake库中,您无法从beforeSave()中删除,因此我将其移至下一个适当的方法,在我的情况下,此方法为beforeValidate( )。

I got this resolved, In the newer libs for cake you can't delete from beforeSave(), so I moved it to the next appropriate method, in my case this was beforeValidate().

希望这对某人有帮助。

这篇关于无法从CakePHP 2.4+中的beforeSave方法删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆