Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed AggregatorObserver delete & forceDelete Methods to respect Aggregator syncing status #301

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/Searchable/AggregatorObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ public function saved($model): void
*/
public function deleted($model): void
{
if (static::syncingDisabledFor($model)) {
if (!$model->wasSearchableBeforeDelete()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check here won't always work. Aggregators can contain classes that don't have the Searchable trait on them. Without this trait, this check will fail because the method is not defined. We should probably add a check on whether or not the model includes the Searchable trait (you can see how here).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know that. Are you sure aggregators work if the underlying models don't have the Searchable trait? The docs for scout extended recommend disabling syncing for models if you don't want them to create their own individual model indexes. If what you say is correct, that wouldn't be needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is something that's possible to do. This is not well documented, but the tests ensure this works as well (in AggregatorTest.php, we use the News aggregator that includes the Post model, which doesn't include the Searchable trait).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. In that case, maybe best to check if the method exists rather than whether the Searchable trait is being used. That way an aggregated class without the Searchable trait can still implement a wasSearchableBeforeDelete method if wanted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that works for me too! 🙂

return;
}

if ($this->usesSoftDelete($model) && config('scout.soft_delete', false)) {
$this->saved($model);
if (config('scout.soft_delete', false) && $this->usesSoftDelete($model)) {
$this->whileForcingUpdate(function () use ($model) {
$this->saved($model);
});
} else {
$class = get_class($model);

Expand All @@ -99,6 +101,9 @@ public function deleted($model): void
}

foreach ($this->aggregators[$class] as $aggregator) {
if (static::syncingDisabledFor($aggregator)) {
return;
}
$aggregator::create($model)->unsearchable();
}
}
Expand All @@ -107,22 +112,21 @@ public function deleted($model): void
/**
* Handle the force deleted event for the model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function forceDeleted($model): void
{
if (static::syncingDisabledFor($model)) {
return;
}

$class = get_class($model);

if (! array_key_exists($class, $this->aggregators)) {
return;
}

foreach ($this->aggregators[$class] as $aggregator) {
if (static::syncingDisabledFor($aggregator)) {
return;
}
$aggregator::create($model)->unsearchable();
}
}
Expand Down