Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove file doesn't work when using folderAsField #33

Open
oscar-b opened this issue Mar 4, 2010 · 4 comments
Open

Remove file doesn't work when using folderAsField #33

oscar-b opened this issue Mar 4, 2010 · 4 comments
Labels

Comments

@oscar-b
Copy link

oscar-b commented Mar 4, 2010

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
Copy link
Author

oscar-b commented Mar 7, 2010

Anyone who can confirm this?

@oscar-b
Copy link
Author

oscar-b commented Mar 9, 2010

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"..

@mrthan
Copy link

mrthan commented Oct 31, 2010

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
Copy link

mrthan commented Oct 31, 2010

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants