This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Vojta Svoboda
Vojta Svoboda

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.

Alexandre Thebaldi
Alexandre Thebaldi

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
Scott

Just set delete to true in your relationship definition.

http://octobercms.com/docs/database/model#relationships

Vojta Svoboda
Vojta Svoboda

Thank you very much, it solve my problem :)

voidshah
voidshah

Scott said:

Just set delete to true in your relationship definition.

http://octobercms.com/docs/database/model#relationships

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

hambern
hambern

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

Tempestronics-Android
Tempestronics-Android

voidshah8960 said:

Scott said:

Just set delete to true in your relationship definition.

http://octobercms.com/docs/database/model#relationships

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.

daftspunky
daftspunky

The delete => true feature is fixed in the stable release coming soon. Using the afterDelete event is the best approach until then.

voidshah
voidshah

daftspunk said:

The delete => true feature is fixed in the stable release coming soon. Using the afterDelete event is the best approach until then.

Excellent!

joseph.d
joseph.d

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.

chocolata
chocolata

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?

Mohsin
Mohsin

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

Publipresse
Publipresse

Same problem than moshin with build 455

1-13 of 13

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.