From e94938d4e6c384649c77067b383f294da81efbac Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 7 Dec 2018 16:37:29 +0100 Subject: [PATCH] Use Model collection where appropriate This way when the model's collection is overwritten it still gets applied. Fixes https://github.com/laravel/scout/issues/188 --- src/Builder.php | 3 +-- src/Engines/AlgoliaEngine.php | 19 +++++++------------ src/Engines/Engine.php | 5 ++--- src/Engines/NullEngine.php | 3 ++- src/Searchable.php | 5 ++--- tests/AlgoliaEngineTest.php | 3 ++- tests/BuilderTest.php | 4 +++- 7 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index 26197a23..94bd668e 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -4,7 +4,6 @@ use Illuminate\Pagination\Paginator; use Illuminate\Support\Traits\Macroable; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Pagination\LengthAwarePaginator; class Builder @@ -267,7 +266,7 @@ public function paginate($perPage = null, $pageName = 'page', $page = null) $perPage = $perPage ?: $this->model->getPerPage(); - $results = Collection::make($engine->map( + $results = $this->model->newCollection($engine->map( $this, $rawResults = $engine->paginate($this, $perPage, $page), $this->model )); diff --git a/src/Engines/AlgoliaEngine.php b/src/Engines/AlgoliaEngine.php index 8b1c4705..f5929bf4 100644 --- a/src/Engines/AlgoliaEngine.php +++ b/src/Engines/AlgoliaEngine.php @@ -4,7 +4,6 @@ use Laravel\Scout\Builder; use AlgoliaSearch\Client as Algolia; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\SoftDeletes; class AlgoliaEngine extends Engine @@ -171,20 +170,16 @@ public function mapIds($results) public function map(Builder $builder, $results, $model) { if (count($results['hits']) === 0) { - return Collection::make(); + return $model->newCollection(); } - $models = $model->getScoutModelsByIds( - $builder, collect($results['hits'])->pluck('objectID')->values()->all() - )->keyBy(function ($model) { - return $model->getScoutKey(); - }); + $objectIds = collect($results['hits'])->pluck('objectID')->values()->all(); - return Collection::make($results['hits'])->map(function ($hit) use ($models) { - if (isset($models[$hit['objectID']])) { - return $models[$hit['objectID']]; - } - })->filter()->values(); + return $model->getScoutModelsByIds( + $builder, $objectIds + )->filter(function ($model) use ($objectIds) { + return in_array($model->getScoutKey(), $objectIds); + }); } /** diff --git a/src/Engines/Engine.php b/src/Engines/Engine.php index f8a09b68..720aa4df 100644 --- a/src/Engines/Engine.php +++ b/src/Engines/Engine.php @@ -3,7 +3,6 @@ namespace Laravel\Scout\Engines; use Laravel\Scout\Builder; -use Illuminate\Database\Eloquent\Collection; abstract class Engine { @@ -94,8 +93,8 @@ public function keys(Builder $builder) */ public function get(Builder $builder) { - return Collection::make($this->map( + return $this->map( $builder, $this->search($builder), $builder->model - )); + ); } } diff --git a/src/Engines/NullEngine.php b/src/Engines/NullEngine.php index 363eb3a3..4638c6d7 100644 --- a/src/Engines/NullEngine.php +++ b/src/Engines/NullEngine.php @@ -4,6 +4,7 @@ use Laravel\Scout\Builder; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Support\Collection as BaseCollection; class NullEngine extends Engine { @@ -61,7 +62,7 @@ public function paginate(Builder $builder, $perPage, $page) */ public function mapIds($results) { - return Collection::make(); + return BaseCollection::make(); } /** diff --git a/src/Searchable.php b/src/Searchable.php index 253c6d52..05ba4444 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -3,7 +3,6 @@ namespace Laravel\Scout; use Laravel\Scout\Jobs\MakeSearchable; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Collection as BaseCollection; @@ -137,7 +136,7 @@ public static function makeAllSearchable() */ public function searchable() { - Collection::make([$this])->searchable(); + $this->newCollection([$this])->searchable(); } /** @@ -159,7 +158,7 @@ public static function removeAllFromSearch() */ public function unsearchable() { - Collection::make([$this])->unsearchable(); + $this->newCollection([$this])->unsearchable(); } /** diff --git a/tests/AlgoliaEngineTest.php b/tests/AlgoliaEngineTest.php index d279e597..3fb2d471 100644 --- a/tests/AlgoliaEngineTest.php +++ b/tests/AlgoliaEngineTest.php @@ -63,7 +63,8 @@ public function test_map_correctly_maps_results_to_models() $model = m::mock('StdClass'); $model->shouldReceive('newQuery')->andReturn($model); $model->shouldReceive('getKeyName')->andReturn('id'); - $model->shouldReceive('getScoutModelsByIds')->andReturn(Collection::make([new AlgoliaEngineTestModel])); + $model->shouldReceive('getScoutModelsByIds')->andReturn($models = Collection::make([new AlgoliaEngineTestModel])); + $model->shouldReceive('newCollection')->andReturn($models); $builder = m::mock(Builder::class); diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 7bc16293..f7e75b4f 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -30,9 +30,11 @@ public function test_pagination_correctly_handles_paginated_results() $model->shouldReceive('searchableUsing')->andReturn($engine = m::mock()); $engine->shouldReceive('paginate'); - $engine->shouldReceive('map')->andReturn(Collection::make([new stdClass])); + $engine->shouldReceive('map')->andReturn($results = Collection::make([new stdClass])); $engine->shouldReceive('getTotalCount'); + $model->shouldReceive('newCollection')->andReturn($results); + $builder->paginate(); }