This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
When I delete some record (from e.g. Users extends Model) which doesn't have soft-deleting trait and having some $attachOne
items (e.g. avatar_image), record is removed, but attachment stay in system_files
table and in /storage folder. So attachment_id
(in system_files) is no longer valid and storage folder grows up.
Is it bug or feature? I can send pull request, but don't know how it should behave. I think all attachment should be deleted, when record is removed and soft-deleting is not active.
For now I solve it by:
public function afterDelete()
{
$this->image->delete();
}
but it's not systematic solution.
if you try something like that, would solve the problem?
$model = User::find(1);
$model->avatar->delete(); // attachment it's removed from system_files table and /storage folder
$model->delete();
Scott said:
Just set
delete
totrue
in your relationship definition.
I've tried like this
public $attachMany = ['photos' => ['System\Models\File', 'delete' => 'true' ]];
and like this
public $attachMany = ['photos' => ['System\Models\File', 'delete' => true ]];
but it does not solve this problem
Last updated
I have created my own Model class in my plugin:
use Model as BaseModel;
class Model extends BaseModel {
protected $dates = ['published_at'];
public $attachMany = [
'images' => ['System\Models\File'],
'files' => ['System\Models\File'],
];
public function afterDelete() {
foreach ($this->images as $image) {
$image->delete();
}
foreach ($this->files as $file) {
$file->delete();
}
}
}
Then I use that as a parent-class for all models. In that way files and images is always deleted after deletion
Last updated
voidshah8960 said:
Scott said:
Just set
delete
totrue
in your relationship definition.I've tried like this
public $attachMany = ['photos' => ['System\Models\File', 'delete' => 'true' ]];
and like this
public $attachMany = ['photos' => ['System\Models\File', 'delete' => true ]];
but it does not solve this problem
Yes but why. I think it has to work since it’s documented. Is something wrong with feature in OctoberCMS or it’s being done wrong.
The delete => true
feature is fixed in the stable release coming soon. Using the afterDelete
event is the best approach until then.
daftspunk said:
The
delete => true
feature is fixed in the stable release coming soon. Using theafterDelete
event is the best approach until then.
Excellent!
Was this ever fixed? I'm using Build 430 and still experiencing this issue.
delete => true
works for regular relations but it isn't working for an attachMany
relation. The afterDelete
workaround described above works, though.
Proposed solution by @hambern works perfectly. Does anyone have an idea how to extend this functionality so that generated thumbnail with getThumb() are cleared aswel? Right now, when deleting a model that has file attachments, orphaned files remain on the server clogging up disk space. The problem is that I cannot find any references to the thumb file in the database... Any advice?
This still doesn't solve some problems. For example, lets say I install the RainLab.User plugin and create a user with an avatar image. When I run php artisan plugin:refresh Rainlab.User
it deletes all the tables but leaves the avatars created. So next time when I create a new user without uploading any image it uses the old image for the same ID 1. This is erratic behaviour right?
Last updated
1-13 of 13