问题
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