Upload::beforeSave() 的声明应该与 Model::beforeSave($options = Array) [APP/Model/Upload.php, line 5] 兼容

Declaration of Upload::beforeSave() should be compatible with Model::beforeSave($options = Array) [APP/Model/Upload.php, line 5]

在我的上传模型中使用 beforeSave 方法时,我在页面顶部显示以下错误。

Strict (2048): Declaration of Upload::beforeSave() should be compatible with Model::beforeSave($options = Array) [APP/Model/Upload.php, line 5]

有人可以指出我做错了什么吗?

这是我的模型:

<?php 

App::uses('AppModel', 'Model');

class Upload extends AppModel {

    protected function _processFile() {
        $file = $this->data['Upload']['file'];
        if ($file['error'] === UPLOAD_ERR_OK) {
            $name = md5($file['name']);
            $path = WWW_ROOT . 'files' . DS . $name;
            if (is_uploaded_file($file['tmp_name'])
                && move_uploaded_file($file['tmp_name'], $path) ) {
                $this->data['Upload']['name'] = $file['name'];
                $this->data['Upload']['size'] = $file['size'];
                $this->data['Upload']['mime'] = $file['type'];
                $this->data['Upload']['path'] = '/files/' . $name;
                unset($this->data['Upload']['file']);
                return true;
            }
        }
        return false;
    }

    public function beforeSave() {
        if (!parent::beforeSave($options)) {
            return false;
        }
        return $this->_processFile();
    }

}

?>

beforeSave() 函数在模型数据成功验证后立即执行,但就在数据保存之前。如果您希望保存操作继续,该函数也应该 return 为真。

此回调对于需要在存储数据之前发生的任何数据处理逻辑特别方便。如果您的存储引擎需要特定格式的日期,请访问 $this->data 并修改它。

下面是一个示例,说明如何使用 beforeSave 进行日期转换。示例中的代码用于数据库中开始日期格式为 YYYY-MM-DD 的应用程序,并在应用程序中显示为 DD-MM-YYYY。当然,这可以很容易地改变。在适当的模型中使用下面的代码。

public function beforeSave($options = array()) {
    if (!empty($this->data['Event']['begindate']) &&
        !empty($this->data['Event']['enddate'])
    ) {

        $this->data['Event']['begindate'] = $this->dateFormatBeforeSave(
            $this->data['Event']['begindate']
        );
        $this->data['Event']['enddate'] = $this->dateFormatBeforeSave(
            $this->data['Event']['enddate']
        );
    }
    return true;
}

public function dateFormatBeforeSave($dateString) {
    return date('Y-m-d', strtotime($dateString));
}

Make sure that beforeSave() returns true, or your save is going to fail.

只需更改此行

public function beforeSave() {

至此,你的方法声明是正确的

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