Skip to content

Commit 7886876

Browse files
authored
Add Configurable Scout Key Type (#752)
* add getScoutKeyType method to allow customizing the key type for scout queries * wip * Enhance testing of SearchableModel's queryScoutModelsByIds method for custom scoutKeyTypes
1 parent 5e883db commit 7886876

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Searchable.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function queryScoutModelsByIds(Builder $builder, array $ids)
253253
call_user_func($builder->queryCallback, $query);
254254
}
255255

256-
$whereIn = in_array($this->getKeyType(), ['int', 'integer']) ?
256+
$whereIn = in_array($this->getScoutKeyType(), ['int', 'integer']) ?
257257
'whereIntegerInRaw' :
258258
'whereIn';
259259

@@ -393,6 +393,16 @@ public function getScoutKey()
393393
return $this->getKey();
394394
}
395395

396+
/**
397+
* Get the auto-incrementing key type for querying models.
398+
*
399+
* @return string
400+
*/
401+
public function getScoutKeyType()
402+
{
403+
return $this->getKeyType();
404+
}
405+
396406
/**
397407
* Get the key name used to index the model.
398408
*

tests/Feature/SearchableTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,36 @@ public function test_was_searchable_on_model_without_soft_deletes()
102102
$this->assertTrue($model->wasSearchableBeforeDelete());
103103
}
104104

105+
public function test_it_queries_searchable_models_by_their_ids_with_integer_key_type()
106+
{
107+
$model = M::mock(SearchableModel::class)->makePartial();
108+
$model->shouldReceive('newQuery')->andReturnSelf();
109+
$model->shouldReceive('getScoutKeyType')->andReturn('int');
110+
$model->shouldReceive('getScoutKeyName')->andReturn('id');
111+
$model->shouldReceive('qualifyColumn')->with('id')->andReturn('qualified_id');
112+
$model->shouldReceive('whereIntegerInRaw')->with('qualified_id', [1, 2, 3])->andReturnSelf();
113+
114+
$scoutBuilder = M::mock(\Laravel\Scout\Builder::class);
115+
$scoutBuilder->queryCallback = null;
116+
117+
$model->queryScoutModelsByIds($scoutBuilder, [1, 2, 3]);
118+
}
119+
120+
public function test_it_queries_searchable_models_by_their_ids_with_string_key_type()
121+
{
122+
$model = M::mock(SearchableModel::class)->makePartial();
123+
$model->shouldReceive('newQuery')->andReturnSelf();
124+
$model->shouldReceive('getScoutKeyType')->andReturn('string');
125+
$model->shouldReceive('getScoutKeyName')->andReturn('id');
126+
$model->shouldReceive('qualifyColumn')->with('id')->andReturn('qualified_id');
127+
$model->shouldReceive('whereIn')->with('qualified_id', [1, 2, 3])->andReturnSelf();
128+
129+
$scoutBuilder = M::mock(\Laravel\Scout\Builder::class);
130+
$scoutBuilder->queryCallback = null;
131+
132+
$model->queryScoutModelsByIds($scoutBuilder, [1, 2, 3]);
133+
}
134+
105135
public function test_was_searchable_before_update_works_from_true_to_false()
106136
{
107137
$model = new SearchableModelWithSoftDeletes([

0 commit comments

Comments
 (0)