From 6afe7a0385a2b9c275dbe1a7f9b033257b680299 Mon Sep 17 00:00:00 2001 From: Fl0Cri Date: Mon, 18 Nov 2024 09:24:57 +0100 Subject: [PATCH] Fix multiple listeners being attached to models Fixes https://github.com/wintercms/winter/issues/1251 As mentioned in the issue above, there's still an edge case if the event is cancelled by another listener. --- src/Database/Model.php | 2 +- src/Database/Traits/SoftDelete.php | 4 ++-- src/Database/Traits/Validation.php | 4 ++-- src/Halcyon/Model.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Database/Model.php b/src/Database/Model.php index 1f7fed123..5d51f021a 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -228,7 +228,7 @@ protected function bootNicerEvents() if ($model->methodExists($method)) { // Register the method as a listener with default priority // to allow for complete control over the execution order - $model->bindEvent('model.' . $method, [$model, $method]); + $model->bindEventOnce('model.' . $method, [$model, $method]); } // First listener that returns a non-null result will cancel the // further propagation of the event; If that result is false, the diff --git a/src/Database/Traits/SoftDelete.php b/src/Database/Traits/SoftDelete.php index 5e372a0ca..960b7f521 100644 --- a/src/Database/Traits/SoftDelete.php +++ b/src/Database/Traits/SoftDelete.php @@ -34,7 +34,7 @@ public static function bootSoftDelete() if ($model->methodExists('beforeRestore')) { // Register the method as a listener with default priority // to allow for complete control over the execution order - $model->bindEvent('model.beforeRestore', [$model, 'beforeRestore']); + $model->bindEventOnce('model.beforeRestore', [$model, 'beforeRestore']); } /** * @event model.beforeRestore @@ -54,7 +54,7 @@ public static function bootSoftDelete() if ($model->methodExists('afterRestore')) { // Register the method as a listener with default priority // to allow for complete control over the execution order - $model->bindEvent('model.afterRestore', [$model, 'afterRestore']); + $model->bindEventOnce('model.afterRestore', [$model, 'afterRestore']); } /** * @event model.afterRestore diff --git a/src/Database/Traits/Validation.php b/src/Database/Traits/Validation.php index ee3ca8ba6..c8c0472fc 100644 --- a/src/Database/Traits/Validation.php +++ b/src/Database/Traits/Validation.php @@ -65,7 +65,7 @@ public static function bootValidation() if ($model->methodExists('beforeValidate')) { // Register the method as a listener with default priority // to allow for complete control over the execution order - $model->bindEvent('model.beforeValidate', [$model, 'beforeValidate']); + $model->bindEventOnce('model.beforeValidate', [$model, 'beforeValidate']); } /** @@ -87,7 +87,7 @@ public static function bootValidation() if ($model->methodExists('afterValidate')) { // Register the method as a listener with default priority // to allow for complete control over the execution order - $model->bindEvent('model.afterValidate', [$model, 'afterValidate']); + $model->bindEventOnce('model.afterValidate', [$model, 'afterValidate']); } /** diff --git a/src/Halcyon/Model.php b/src/Halcyon/Model.php index f51c3ac47..5a9ff66d0 100644 --- a/src/Halcyon/Model.php +++ b/src/Halcyon/Model.php @@ -242,7 +242,7 @@ protected function bootNicerEvents() if ($model->methodExists($method)) { // Register the method as a listener with default priority // to allow for complete control over the execution order - $model->bindEvent('model.' . $method, [$model, $method]); + $model->bindEventOnce('model.' . $method, [$model, $method]); } // First listener that returns a non-null result will cancel the // further propagation of the event; If that result is false, the