From 87f4716ed72231c32cc00e479325970a4c8435d7 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Wed, 18 Oct 2023 14:53:27 +0100 Subject: [PATCH] fix: custom eloquent events --- framework/core/src/Database/AbstractModel.php | 10 ++++++++++ framework/core/src/Discussion/Discussion.php | 12 ++++++++---- framework/core/src/Search/Listener/ModelObserver.php | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/framework/core/src/Database/AbstractModel.php b/framework/core/src/Database/AbstractModel.php index 7a4679d752..812952fbc9 100644 --- a/framework/core/src/Database/AbstractModel.php +++ b/framework/core/src/Database/AbstractModel.php @@ -237,4 +237,14 @@ public function newCollection(array $models = []): Collection { return new Collection($models); } + + public function __sleep() + { + // Closures cannot be serialized. + // We should not need them if we are serializing a model. + $this->afterSaveCallbacks = []; + $this->afterDeleteCallbacks = []; + + return parent::__sleep(); + } } diff --git a/framework/core/src/Discussion/Discussion.php b/framework/core/src/Discussion/Discussion.php index 4ec7d2b526..cf1b8c721a 100644 --- a/framework/core/src/Discussion/Discussion.php +++ b/framework/core/src/Discussion/Discussion.php @@ -146,8 +146,10 @@ public function hide(?User $actor = null): static $this->raise(new Hidden($this)); - $this->afterSave(function () { - $this->fireModelEvent('hidden', false); + $this->saved(function (self $model) { + if ($model === $this) { + $model->fireModelEvent('hidden', false); + } }); } @@ -162,8 +164,10 @@ public function restore(): static $this->raise(new Restored($this)); - $this->afterSave(function () { - $this->fireModelEvent('restored', false); + $this->saved(function (self $model) { + if ($model === $this) { + $model->fireModelEvent('restored', false); + } }); } diff --git a/framework/core/src/Search/Listener/ModelObserver.php b/framework/core/src/Search/Listener/ModelObserver.php index b47acbb008..58bcf0f712 100644 --- a/framework/core/src/Search/Listener/ModelObserver.php +++ b/framework/core/src/Search/Listener/ModelObserver.php @@ -54,7 +54,7 @@ public function restored(AbstractModel $model): void private function runIndexJob(AbstractModel $model, string $operation): void { - if ($this->search->indexable($model)) { + if ($this->search->indexable($model::class)) { foreach ($this->search->indexers($model::class) as $indexerClass) { $queue = property_exists($indexerClass, 'queue') ? $indexerClass::$queue : null;