From 3fb97f479d2cc4c82796fcc4c71658ad827b644f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=8D=9A?= Date: Thu, 29 Aug 2019 14:30:50 +0800 Subject: [PATCH 1/3] optimized findBy function --- .../Contracts/RepositoryInterface.php | 6 ++- .../Repositories/Eloquent/Repository.php | 40 +++++++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/Uniqueway/Repositories/Contracts/RepositoryInterface.php b/src/Uniqueway/Repositories/Contracts/RepositoryInterface.php index bc60bd3..17dbe46 100644 --- a/src/Uniqueway/Repositories/Contracts/RepositoryInterface.php +++ b/src/Uniqueway/Repositories/Contracts/RepositoryInterface.php @@ -44,11 +44,13 @@ public function find($id, $columns = ['*']); /** * @param $attribute - * @param $value + * @param string $value * @param array $columns + * @param bool $or * @return mixed + * @author Taylor */ - public function findBy($attribute, $value = '', $columns = ['*']); + public function findBy($attribute, $value = '', $columns = ['*'], $or = false); /** * @param $field diff --git a/src/Uniqueway/Repositories/Eloquent/Repository.php b/src/Uniqueway/Repositories/Eloquent/Repository.php index 25f17c6..47ca996 100644 --- a/src/Uniqueway/Repositories/Eloquent/Repository.php +++ b/src/Uniqueway/Repositories/Eloquent/Repository.php @@ -152,15 +152,49 @@ public function find($id, $columns = ['*']) * @param array $columns * @return mixed */ - public function findBy($attribute, $value = '', $columns = ['*']) + /** + * @param $attribute + * @param string $value + * @param array $columns + * @param bool $or + * @return mixed + * @author Taylor + */ + public function findBy($attribute, $value = '', $columns = ['*'], $or = false) { $this->applyCriteria(); + $model = $this->model; + if (is_array($attribute)) { - return $this->model->where($attribute)->first($columns); + foreach ($attribute as $field => $value) { + if ($value instanceof \Closure) { + $model = (!$or) + ? $model->where($value) + : $model->orWhere($value); + } elseif (is_array($value)) { + if (count($value) === 3) { + list($field, $operator, $search) = $value; + $model = (!$or) + ? $model->where($field, $operator, $search) + : $model->orWhere($field, $operator, $search); + } elseif (count($value) === 2) { + list($field, $search) = $value; + $model = (!$or) + ? $model->where($field, '=', $search) + : $model->orWhere($field, '=', $search); + } + } else { + $model = (!$or) + ? $model->where($field, '=', $value) + : $model->orWhere($field, '=', $value); + } + } + + return $model->first($columns); } - return $this->model->where($attribute, '=', $value)->first($columns); + return $model->where($attribute, '=', $value)->first($columns); } /** From cd43bed953aadcde1b93d3f82c42b47553d35c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=8D=9A?= Date: Mon, 2 Sep 2019 12:57:51 +0800 Subject: [PATCH 2/3] Remove useless code. --- .../Contracts/RepositoryInterface.php | 6 +- .../Criteria/FuzzyGroupSearch.php | 58 +++++++++++++++++++ .../Repositories/Eloquent/Repository.php | 40 +------------ 3 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php diff --git a/src/Uniqueway/Repositories/Contracts/RepositoryInterface.php b/src/Uniqueway/Repositories/Contracts/RepositoryInterface.php index 17dbe46..bc60bd3 100644 --- a/src/Uniqueway/Repositories/Contracts/RepositoryInterface.php +++ b/src/Uniqueway/Repositories/Contracts/RepositoryInterface.php @@ -44,13 +44,11 @@ public function find($id, $columns = ['*']); /** * @param $attribute - * @param string $value + * @param $value * @param array $columns - * @param bool $or * @return mixed - * @author Taylor */ - public function findBy($attribute, $value = '', $columns = ['*'], $or = false); + public function findBy($attribute, $value = '', $columns = ['*']); /** * @param $field diff --git a/src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php b/src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php new file mode 100644 index 0000000..ab09d6e --- /dev/null +++ b/src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php @@ -0,0 +1,58 @@ +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/Eloquent/Repository.php b/src/Uniqueway/Repositories/Eloquent/Repository.php index 47ca996..25f17c6 100644 --- a/src/Uniqueway/Repositories/Eloquent/Repository.php +++ b/src/Uniqueway/Repositories/Eloquent/Repository.php @@ -152,49 +152,15 @@ public function find($id, $columns = ['*']) * @param array $columns * @return mixed */ - /** - * @param $attribute - * @param string $value - * @param array $columns - * @param bool $or - * @return mixed - * @author Taylor - */ - public function findBy($attribute, $value = '', $columns = ['*'], $or = false) + public function findBy($attribute, $value = '', $columns = ['*']) { $this->applyCriteria(); - $model = $this->model; - if (is_array($attribute)) { - foreach ($attribute as $field => $value) { - if ($value instanceof \Closure) { - $model = (!$or) - ? $model->where($value) - : $model->orWhere($value); - } elseif (is_array($value)) { - if (count($value) === 3) { - list($field, $operator, $search) = $value; - $model = (!$or) - ? $model->where($field, $operator, $search) - : $model->orWhere($field, $operator, $search); - } elseif (count($value) === 2) { - list($field, $search) = $value; - $model = (!$or) - ? $model->where($field, '=', $search) - : $model->orWhere($field, '=', $search); - } - } else { - $model = (!$or) - ? $model->where($field, '=', $value) - : $model->orWhere($field, '=', $value); - } - } - - return $model->first($columns); + return $this->model->where($attribute)->first($columns); } - return $model->where($attribute, '=', $value)->first($columns); + return $this->model->where($attribute, '=', $value)->first($columns); } /** 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 3/3] =?UTF-8?q?Change=20FuzzySearch.=20Diffrent=20from=20p?= =?UTF-8?q?revious:=20Old:=20```=20$this->userRepository->pushCriteria(new?= =?UTF-8?q?=20FuzzySearch('Jack',=20[=20'name',=20'pinyin'=20]));=20$this-?= =?UTF-8?q?>userRepository->pushCriteria(new=20AttributeSearch(['city'=20?= =?UTF-8?q?=3D>=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; + }); } }