MeioUpload icon indicating copy to clipboard operation
MeioUpload copied to clipboard

Remove file doesn't work when using folderAsField

Open oscar-b opened this issue 14 years ago • 4 comments

The only place folderAsField is considered is when uploading.

In setup(), $options['dir'] doesn't get this extra field which leads to afterSave() calling _deleteFiles() only with that dir, missing for example the model id at the end.

oscar-b avatar Mar 04 '10 16:03 oscar-b

Anyone who can confirm this?

oscar-b avatar Mar 07 '10 23:03 oscar-b

Been thinking a little about this.. Wouldn't it be more flexible to enhance _replaceTokens to handle tokens like {Model.field}? That way, you could construct the path exactly as you wish, and you'd get rid of the clunky "folderAsField"..

oscar-b avatar Mar 09 '10 15:03 oscar-b

Hi,

Yes, this is a problem. I was just going to add an issue. Currently, only the method _uploadFile deals with the folderAsField setting. Part of the problem maybe because it relies on model data from the DB, so it's probably not always available when setup() is run.

When delete() comes around, it's only got this set (from setup()) $options['dir'] = rtrim($this->_replaceTokens($model, $options['dir'], $field, $tokens), DS);

and possibly doesn't have $data[$model->alias][$options['folderAsField']] from __upload() available (all the time).

mrthan avatar Oct 31 '10 12:10 mrthan

I know this isn't the way to do it, but my git skills aren't too flash. This fixes the issue, possibly not the nicest but does the trick for now, beforeDelete already reads the data so it's just a matter of using it again:

function beforeDelete(&$model) {
    $model->read(null, $model->id);
    if (isset($model->data)) {
        foreach ($this->__fields[$model->alias] as $field => $options) {
            //recover the extra bit of the filename
            if (isset($options['folderAsField']) && !empty($options['folderAsField']) && is_string($options['folderAsField'])) {
                $options['dir'] = $options['dir'] . DS . $model->data[$model->alias][$options['folderAsField']];
                $this->__fields[$model->alias][$field]['dir'] = $options['dir'];
            }
            $this->_setFileToRemove($model, $field);
        }
    }
    return true;
}

mrthan avatar Oct 31 '10 12:10 mrthan