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

2

6

I am being shown the following error on top of my page when using beforeSave method in my Upload model.

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

Could someone point out what I'm doing wrong?

Here is my model:

<?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();
    }

}

?>
Herpetology answered 26/4, 2015 at 1:4 Comment(0)
T
14

Just change this line

public function beforeSave() {

to this, so you have correct method declaration

public function beforeSave($options = array()) {
Tarratarradiddle answered 26/4, 2015 at 9:10 Comment(0)
P
2

The beforeSave() function executes immediately after model data has been successfully validated, but just before the data is saved. This function should also return true if you want the save operation to continue.

This callback is especially handy for any data-massaging logic that needs to happen before your data is stored. If your storage engine needs dates in a specific format, access it at $this->data and modify it.

Below is an example of how beforeSave can be used for date conversion. The code in the example is used for an application with a begindate formatted like YYYY-MM-DD in the database and is displayed like DD-MM-YYYY in the application. Of course this can be changed very easily. Use the code below in the appropriate model.

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.

Pile answered 26/4, 2015 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.