Skip to content

Commit

Permalink
Change FuzzySearch.
Browse files Browse the repository at this point in the history
Diffrent from previous:
Old:
```
$this->userRepository->pushCriteria(new FuzzySearch('Jack', [ 'name', 'pinyin' ]));
$this->userRepository->pushCriteria(new AttributeSearch(['city' => '北京']));

// sql executed: select * from `users` where `name` LIKE "%Jack%" OR `pinyin` LIKE "%%Jack" AND `city` = " 北京";
```
New
```
$this->userRepository->pushCriteria(new FuzzySearch('Jack', [ 'name', 'pinyin' ]));
$this->userRepository->pushCriteria(new AttributeSearch(['city' => '北京']));

// sql executed: select * from `users` where (`name` LIKE "%Jack%" OR `pinyin` LIKE "%%Jack") AND `city` = " 北京";
```
  • Loading branch information
DevDynamo2024 committed Sep 2, 2019
1 parent cd43bed commit 9da117f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 70 deletions.
58 changes: 0 additions & 58 deletions src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php

This file was deleted.

21 changes: 9 additions & 12 deletions src/Uniqueway/Repositories/Criteria/FuzzySearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,15 @@ public function apply($model, Repository $repository)
return $model;
}

$where = 'where';
foreach ($this->attributes as $attribute) {
if ($attribute == 'id' && preg_match('/^\d+$/', $this->query)) {
$model = $model->$where($attribute, '=', $this->query);
} else {
$pattern = '%' . $this->query . '%';
$model = $model->$where($attribute, 'LIKE', $pattern);
return $model->where(function ($query) {
foreach ($this->attributes as $attribute) {
if ($attribute == 'id' && preg_match('/^\d+$/', $this->query)) {
$query->orWhere($attribute, '=', $this->query);
} else {
$pattern = '%' . $this->query . '%';
$query->orWhere($attribute, 'LIKE', $pattern);
}
}

$where = 'orWhere';
}

return $model;
});
}
}

0 comments on commit 9da117f

Please sign in to comment.