From 9da117f73cdc7c39a99d6de225554c74d5931e5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=8D=9A?= Date: Mon, 2 Sep 2019 13:44:32 +0800 Subject: [PATCH] =?UTF-8?q?Change=20FuzzySearch.=20Diffrent=20from=20previ?= =?UTF-8?q?ous:=20Old:=20```=20$this->userRepository->pushCriteria(new=20F?= =?UTF-8?q?uzzySearch('Jack',=20[=20'name',=20'pinyin'=20]));=20$this->use?= =?UTF-8?q?rRepository->pushCriteria(new=20AttributeSearch(['city'=20=3D>?= =?UTF-8?q?=20'=E5=8C=97=E4=BA=AC']));?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit // 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` = " 北京"; ``` --- .../Criteria/FuzzyGroupSearch.php | 58 ------------------- .../Repositories/Criteria/FuzzySearch.php | 21 +++---- 2 files changed, 9 insertions(+), 70 deletions(-) delete mode 100644 src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php diff --git a/src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php b/src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php deleted file mode 100644 index ab09d6e..0000000 --- a/src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php +++ /dev/null @@ -1,58 +0,0 @@ -query = $query; - $this->attributes = $attributes; - } - - /** - * @param $model - * @param Repository $repository - * @return mixed - */ - public function apply($model, Repository $repository) - { - if (empty($this->query)) { - return $model; - } - - 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); - } - } - }); - } -} diff --git a/src/Uniqueway/Repositories/Criteria/FuzzySearch.php b/src/Uniqueway/Repositories/Criteria/FuzzySearch.php index 86d5090..6319fe2 100644 --- a/src/Uniqueway/Repositories/Criteria/FuzzySearch.php +++ b/src/Uniqueway/Repositories/Criteria/FuzzySearch.php @@ -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; + }); } }