CakePHP-FileUpload-Plugin
CakePHP-FileUpload-Plugin copied to clipboard
When adding file upload fields to existing model, if no file is supplied, the entire Model portion of the array is unset
so if i have a model Books and i want have an image for each book, without usign a sepreate table to store the book images
Array
(
[Book] => Array
(
[id] => 348
[name] => Some Book Name
[file] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
)
if no file has been uploaded (either because none was chosen or one already exists) then it unsets the entire model rather than just the [file] portion
this patch introduces a allowEmpty option which defaults to false, but if true will only unset the [file] portion allowing any changes to the [name] to be saved
diff --git a/config/file_upload_settings.php b/config/file_upload_settings.php
old mode 100644
new mode 100755
index 2b08d42..ea2cc8f
--- a/config/file_upload_settings.php
+++ b/config/file_upload_settings.php
@@ -187,7 +187,14 @@ class FileUploadSettings {
\* 'fileNameFunction' => 'md5'
\* 'fileNameFunction' => 'crc32'
*/
- 'fileNameFunction' => false
- 'fileNameFunction' => false,
+
- /**
- \* To allow Model to be saved without the file fields if no file is uploaded.
- *
- \* For use when using FileUpload directly on an existing Model.
- */
- ```
'allowEmpty' => false
```
);
}
diff --git a/models/behaviors/file_upload.php b/models/behaviors/file_upload.php
old mode 100644
new mode 100755
index 040b5d1..21c6418
--- a/models/behaviors/file_upload.php
+++ b/models/behaviors/file_upload.php
@@ -76,7 +76,12 @@ class FileUploadBehavior extends ModelBehavior {
unset($Model->data[$Model->alias][$this->options[$Model->alias]['fileVar']]);
}
else {
- unset($Model->data[$Model->alias]);
- if ($this->options[$Model->alias]['allowEmpty']) {
- //unset only the file portion of the model so that the rest fo the model's data will be saved
- unset($Model->data[$Model->alias]['file']);
- } else {
- unset($Model->data[$Model->alias]);
- }
}
}
return $Model->beforeSave();