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

Cleanup after model is deleted #81

Merged
merged 6 commits into from
Jun 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion behaviors/TranslatableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,32 @@ public function __construct($model)

$model->morphMany['translations'] = [
'Winter\Translate\Models\Attribute',
'name' => 'model'
'name' => 'model',
];

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

public function afterModelDelete()
{
if ($this->model->methodExists('isSoftDelete') && $this->model->isSoftDelete()) {
return;
}

$model_id = $this->model->getKey();
$model_type = $this->getClass();

// delete translation attributes for this record
Db::table('winter_translate_attributes')
->where('model_id', $model_id)
->where('model_type', $model_type)
->delete();

// delete translation indexes for this record
Db::table('winter_translate_indexes')
->where('model_id', $model_id)
->where('model_type', $model_type)
->delete();
}

/**
Expand Down
Loading