Skip to content

Commit

Permalink
allow deletion of a file without deleting whole record
Browse files Browse the repository at this point in the history
  • Loading branch information
szajbus committed Aug 30, 2012
1 parent 3df654c commit cc102b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Model/Behavior/UploadBehavior.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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_name'] = $this->toWrite[$field]['name'];
$model->data[$model->name][$field.'_file_size'] = $this->toWrite[$field]['size']; $model->data[$model->name][$field.'_file_size'] = $this->toWrite[$field]['size'];
$model->data[$model->name][$field.'_content_type'] = $this->toWrite[$field]['type']; $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; return true;
Expand Down
14 changes: 10 additions & 4 deletions README.textile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class UsersController extends AppController {
var $name = 'Users'; var $name = 'Users';
var $uses = array('User'); var $uses = array('User');
var $helpers = array('Form', 'UploadPack.Upload'); var $helpers = array('Form', 'UploadPack.Upload');

function create() { function create() {
if (!empty($this->data)) { if (!empty($this->data)) {
$this->User->create($this->data); $this->User->create($this->data);
Expand All @@ -78,7 +78,7 @@ class UsersController extends AppController {
} }
} }
} }

function show($id) { function show($id) {
$this->set('user', $this->User->findById($id)); $this->set('user', $this->User->findById($id));
} }
Expand Down Expand Up @@ -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. 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 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. Styles are the definition of thumbnails that will be generated for original image. You can define as many as you want.
Expand Down Expand Up @@ -322,7 +329,7 @@ Returns url to the uploaded file
h4. UploadHelper::image($data, $field, $options = array(), $htmlOptions = array()) h4. UploadHelper::image($data, $field, $options = array(), $htmlOptions = array())


Returns image tag pointing to uploaded file. Returns image tag pointing to uploaded file.

* *$data* - record from database (would be @$user@ here) * *$data* - record from database (would be @$user@ here)
* *$field* - name of a field, like this @Modelname.fieldname@ (would be @User.avatar@ here) * *$field* - name of a field, like this @Modelname.fieldname@ (would be @User.avatar@ here)
* *$options* - url options * *$options* - url options
Expand Down Expand Up @@ -375,7 +382,6 @@ h2. Plans for future
There is still something missing in UploadPack, here's what will be added soon: There is still something missing in UploadPack, here's what will be added soon:


* file name normalization * file name normalization
* allow deletion of a file without deleting whole record


The plans for more distant future: The plans for more distant future:


Expand Down

0 comments on commit cc102b0

Please sign in to comment.