Download declaration :: beforeSave () must be compatible with Model :: beforeSave ($ options = Array) [APP / Model / Upload.php, line 5]

When using the beforeSave method in my loading model, the following error appears on top of my page.

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

Can 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(); } } ?> 
+5
source share
2 answers

Just change this line

 public function beforeSave() { 

so you have the correct method declaration

 public function beforeSave($options = array()) { 
+11
source

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

This callback is especially convenient for any data processing logic that must occur before your data is saved. If your storage engine needs dates in a specific format, refer to it in $ this-> data and change it.

The following is an example of how beforeSave can be used to convert dates. The code in the example is used for the indented application in the YYYY-MM-DD format in the database and is displayed as DD-MM-YYYY in the application. Of course, this can be easily changed. 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('Ym-d', strtotime($dateString)); } 

Make sure beforeSave () returns true, or your save will fail.

+2
source

All Articles