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

Translate attributes / indexes not cleared when model is deleted #652

Closed
acasar opened this issue Sep 1, 2021 · 5 comments
Closed

Translate attributes / indexes not cleared when model is deleted #652

acasar opened this issue Sep 1, 2021 · 5 comments

Comments

@acasar
Copy link
Contributor

acasar commented Sep 1, 2021

Translatable attributes/indexes are not removed from the database after a parent model is deleted, which can cause issues in some situations when using transWhere.

This was already reported (#211), but I am reopening the issue, since it has not been resolved and it's causing our production apps to fail in unexpected ways

@daftspunk
Copy link
Member

daftspunk commented Sep 2, 2021

Correct me if I'm wrong, but could this be caused by 508c42c since it happens across two separate queries now. One idea is to use GC to clean up orphaned records.

@acasar
Copy link
Contributor Author

acasar commented Sep 2, 2021

That's true, when implementing 508c42c I was not aware that translation tables contain orphaned records by design. But I think that a cleanup of related translated records in beforeDelete event should solve this issue?

@daftspunk
Copy link
Member

daftspunk commented Sep 8, 2021

Yes, if you are happy with that, let's hook afterDelete inside \RainLab\Translate\Behaviors\TranslatableModel to clean up orphaned indexes.

public function __construct($model)
{
    parent::__construct($model);

    // ...

    $model->bindEvent('model.afterDelete', [$this, 'cleanupTranslatableData']);
}

@acasar
Copy link
Contributor Author

acasar commented Sep 23, 2021

If anyone else is interested, I fixed this by extending the base model:

$model->bindEvent('model.beforeDelete', function() use ($model) {
    if($model->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')) {
        DB::table('rainlab_translate_attributes')
            ->where('model_id', $model->getKey())
            ->where('model_type', get_class($model))
            ->delete();
        DB::table('rainlab_translate_indexes')
            ->where('model_id', $model->getKey())
            ->where('model_type', get_class($model))
            ->delete();
    }
});

And I also added a console command to clean existing orphaned records.

daftspunk added a commit that referenced this issue Sep 25, 2021
@daftspunk
Copy link
Member

Looks good, native integration added to the behavior here: d4eb433

Thanks @acasar!

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

No branches or pull requests

2 participants