-
Notifications
You must be signed in to change notification settings - Fork 254
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
Enhancement - Delete file during/after edit #493
Comments
I also write afterSave method with almost afterDelete code at Upload.Upload behavior. /**
* Deletes the original files after the entity is saved
*
* @param Event $event
* @param Entity $entity
* @param ArrayObject $options
* @return bool
*/
public function afterSave(Event $event, Entity $entity, ArrayObject $options)
{
$result = true;
$behavior = $this->getBehavior('Upload');
foreach ($behavior->getConfig(null, []) as $field => $settings) {
if ($entity->isNew()) {
continue;
}
if (!$entity->isDirty($field)) {
continue;
}
if (Hash::get($settings, 'keepFilesOnDelete', true)) {
continue;
}
$dirField = Hash::get($settings, 'fields.dir', 'dir');
if ($entity->has($dirField)) {
$path = $entity->get($dirField);
} else {
$path = $behavior->getPathProcessor($entity, $entity->get($field), $field, $settings)->basepath();
}
$original_entity = clone $entity;
$original_entity->{$field} = $entity->getOriginal($field);
$callback = Hash::get($settings, 'deleteCallback', null);
if ($callback && is_callable($callback)) {
$files = $callback($path, $original_entity, $field, $settings);
} else {
$files = [$path . $original_entity->get($field)];
}
$writer = $behavior->getWriter($entity, [], $field, $settings);
$success = $writer->delete($files);
if ($result && (new Collection($success))->contains(false)) {
$result = false;
}
}
return $result;
} |
b0n, Where should i place your afterSave() method? I checked and see those method are public, but somehow my model can not call them. Please kindly advice. |
Hello general! I use Josegonzalez/cakephp-upload 3.0. Please checkout and try these. And Josegonzalez/cakephp-upload 6.0 has these methods. Please check this out. https://github.com/FriendsOfCake/cakephp-upload/blob/6.0.0/src/Model/Behavior/UploadBehavior.php |
Hi I recently noticed an issue that files where not being deleted when I was editing a table. (IE: User changes their profile photo.) I have the plugin uploading the file, updating the database but the old file doesn't get deleted.
Note:
I changed the path in the config to:
So here is my temporary solution in the model but I imagine there is a better way to accomplish this.
The text was updated successfully, but these errors were encountered: