Skip to content

Commit

Permalink
fix: custom eloquent events
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Oct 18, 2023
1 parent 52043a9 commit 87f4716
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 10 additions & 0 deletions framework/core/src/Database/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
12 changes: 8 additions & 4 deletions framework/core/src/Discussion/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

Expand All @@ -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);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion framework/core/src/Search/Listener/ModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 87f4716

Please sign in to comment.