This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
chris10207
All in the subject : How to detect when a relation instance is added or deleted from a model ?
My test case is simple: I need to detect when a model 'project' is added/deleted to a model 'developer', A developer belongsToMany projects
I know about the beforeSave() events method but that does not seems to apply on a relation.
Thanks for any help
chris10207
I found this approach working quite well
in your plugin.php, I need to create a log entry when a file is uploaded or remove from a relationship on a model Round
function boot() {
\System\Models\File::extend(function($model) {
$model->bindEvent('model.afterSave', function() use ($model) {
if('XYZ\Models\Round' == $model->attachment_type) {
if ($round = Round::find($model->attachment_id)) {
$round->addLogEntry('File added');
$round->save();
}
}
});
$model->bindEvent('model.afterDelete', function() use ($model) {
if('XYZ\Models\Round' == $model->attachment_type) {
if ($round = Round::find($model->attachment_id)) {
$round->addLogEntry('File removed');
$round->save();
}
}
});
});
}
1-2 of 2