Skip to content

fix exception in collection if model use soft delete #6

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

Merged
merged 4 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/PostgresEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
->whereRaw("$indexColumn @@ query")
->orderBy('rank', 'desc')
->orderBy($builder->model->getKeyName());

//if model use soft delete - without trashed
if (method_exists($builder->model, 'getDeletedAtColumn')) {
$query->where($builder->model->getDeletedAtColumn(), null);
}
if ($perPage > 0) {
$query->skip(($page - 1) * $perPage)
->limit($perPage);
Expand Down
76 changes: 76 additions & 0 deletions tests/PostgresEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function test_update_adds_object_to_index()
$table->shouldReceive('where')
->with('id', '=', 1)
->andReturnSelf();

$table->shouldReceive('update')
->with(['searchable' => 'foo']);

Expand Down Expand Up @@ -98,6 +99,25 @@ public function test_search()
$engine->search($builder);
}

public function test_search_with_soft_delete()
{
list($engine, $db) = $this->getEngine();

$table = $this->setDbExpectations($db);

$table->shouldReceive('skip')->with(0)->andReturnSelf()
->shouldReceive('limit')->with(5)->andReturnSelf()
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
->shouldReceive('where')->with('deleted_at', null);

$db->shouldReceive('select')->with(null, ['foo', 1]);

$builder = new Builder(new TestWithSoftDeleteModel(), 'foo');
$builder->where('bar', 1)->take(5);

$engine->search($builder);
}

public function test_map_correctly_maps_results_to_models()
{
list($engine) = $this->getEngine();
Expand Down Expand Up @@ -227,3 +247,59 @@ public function searchableAdditionalArray()
return $this->searchableAdditionalArray;
}
}

class TestWithSoftDeleteModel extends Model
{
use \Illuminate\Database\Eloquent\SoftDeletes;
public $id = 1;

public $text = 'Foo';

protected $searchableOptions = [
'rank' => [
'fields' => [
'nullable' => 'B',
],
],
];

protected $searchableAdditionalArray = [];

public function searchableAs()
{
return 'searchable';
}

public function getKeyName()
{
return 'id';
}

public function getKey()
{
return $this->id;
}

public function getTable()
{
return 'table';
}

public function toSearchableArray()
{
return [
'text' => $this->text,
'nullable' => null,
];
}

public function searchableOptions()
{
return $this->searchableOptions;
}

public function searchableAdditionalArray()
{
return $this->searchableAdditionalArray;
}
}