Skip to content

Commit

Permalink
Cleanup after model is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin authored Jun 3, 2024
1 parent 52bfc8a commit 6d14cf7
Showing 1 changed file with 25 additions and 1 deletion.
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

0 comments on commit 6d14cf7

Please sign in to comment.