Can't delete row from beforeSave method in CakePHP 2.4+

不问归期 提交于 2019-12-12 04:57:55

问题


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?

Code:

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

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

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

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

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

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

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

Any help at all would be great.


回答1:


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().

Hope this helps someone.



来源:https://stackoverflow.com/questions/25670891/cant-delete-row-from-beforesave-method-in-cakephp-2-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!