Permalink
Browse files

allow deletion of a file without deleting whole record

  • Loading branch information...
1 parent 3df654c commit cc102b08822ff8d9842d8bd63794445da79c07d6 @szajbus committed Aug 30, 2012
Showing with 15 additions and 4 deletions.
  1. +5 −0 Model/Behavior/UploadBehavior.php
  2. +10 −4 README.textile
@@ -47,6 +47,11 @@ public function beforeSave(&$model) {
$model->data[$model->name][$field.'_file_name'] = $this->toWrite[$field]['name'];
$model->data[$model->name][$field.'_file_size'] = $this->toWrite[$field]['size'];
$model->data[$model->name][$field.'_content_type'] = $this->toWrite[$field]['type'];
+ } elseif ($model->data[$model->name][$field] === null) {
+ if (!empty($model->id)) {
+ $this->_prepareToDeleteFiles($model, $field, true);
+ }
+ unset($model->data[$model->name][$field]);
}
}
return true;
View
@@ -69,7 +69,7 @@ class UsersController extends AppController {
var $name = 'Users';
var $uses = array('User');
var $helpers = array('Form', 'UploadPack.Upload');
-
+
function create() {
if (!empty($this->data)) {
$this->User->create($this->data);
@@ -78,7 +78,7 @@ class UsersController extends AppController {
}
}
}
-
+
function show($id) {
$this->set('user', $this->User->findById($id));
}
@@ -259,6 +259,13 @@ h4. You can choose another field for an external url
This way the user can paste an url or choose a file from their filesystem. The url will mimick usual uploading so it will still be validated and resized.
+h4. Deleting a file attached to field
+
+<pre><code>
+ $model->data['avatar'] = null;
+ $model->save();
+</code></pre>
+
h3. More on styles
Styles are the definition of thumbnails that will be generated for original image. You can define as many as you want.
@@ -322,7 +329,7 @@ Returns url to the uploaded file
h4. UploadHelper::image($data, $field, $options = array(), $htmlOptions = array())
Returns image tag pointing to uploaded file.
-
+
* *$data* - record from database (would be @$user@ here)
* *$field* - name of a field, like this @Modelname.fieldname@ (would be @User.avatar@ here)
* *$options* - url options
@@ -375,7 +382,6 @@ h2. Plans for future
There is still something missing in UploadPack, here's what will be added soon:
* file name normalization
-* allow deletion of a file without deleting whole record
The plans for more distant future:

0 comments on commit cc102b0

Please sign in to comment.