-
Notifications
You must be signed in to change notification settings - Fork 357
Fix model serialization issue in RemoveFromSearch job when using custom Scout key values #655
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
Changes from all commits
19a79c0
cb7b1b2
90783ce
90a74d4
246bf96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,20 +54,20 @@ public function test_models_are_deserialized_without_the_database() | |
| $this->assertEquals(1234, $job->models->first()->getScoutKey()); | ||
| } | ||
|
|
||
| public function test_models_are_deserialized_without_the_database_using_custom_scout_key() | ||
| public function test_models_are_deserialized_without_the_database_using_custom_scout_key_values() | ||
| { | ||
| $job = new RemoveFromSearch(Collection::make([ | ||
| $model = new SearchableModelWithCustomKey(['other_id' => 1234]), | ||
| $model = new MeiliSearchCustomKeySearchableModel(['id' => 1234]), | ||
| ])); | ||
|
|
||
| $job = unserialize(serialize($job)); | ||
|
|
||
| $this->assertInstanceOf(Collection::class, $job->models); | ||
| $this->assertCount(1, $job->models); | ||
| $this->assertInstanceOf(SearchableModelWithCustomKey::class, $job->models->first()); | ||
| $this->assertInstanceOf(MeiliSearchCustomKeySearchableModel::class, $job->models->first()); | ||
| $this->assertTrue($model->is($job->models->first())); | ||
| $this->assertEquals(1234, $job->models->first()->getScoutKey()); | ||
| $this->assertEquals('searchable_model_with_custom_keys.other_id', $job->models->first()->getScoutKeyName()); | ||
| $this->assertEquals('my-meilisearch-key.1234', $job->models->first()->getScoutKey()); | ||
| $this->assertEquals('meili_search_custom_key_searchable_models.id', $job->models->first()->getScoutKeyName()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test will pass because the public function getScoutKey()
{
return 'my-meilisearch-key.'.$this->getKey();
}If another model attribute is used to construct the scout key, then this will fail, as it cannot be retrieved from the faux model that is created after the job is unserialized. All other model properties will not exist. |
||
| } | ||
|
|
||
| public function test_removeable_scout_collection_returns_scout_keys() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't make this change. The
RemoveableScoutCollectioninstance must be present in the constructor for thegetQueueableIds()method to return the proper scout keys. This collection method will be called when the job is serialized and placed onto the queue:scout/src/Jobs/RemoveableScoutCollection.php
Line 15 in 3352fbc
The test passes because the
Eloquent\Collectioninstance will (by default) utilize the model's primary key (which is being populated in the test model) in it'sgetQueuableIds()method, so it will be available inside of the faux model, and thus thegetScoutKey()will return the correct key:https://github.com/laravel/framework/blob/b89f0d95275baf8a2b5adcc265203d7d365950be/src/Illuminate/Database/Eloquent/Collection.php#L697-L706