From 56573480d8031450f3932ac08ff00fff16e162ef Mon Sep 17 00:00:00 2001 From: cexll Date: Tue, 26 Apr 2022 11:21:15 +0800 Subject: [PATCH] =?UTF-8?q?faet:=20where=E5=A2=9E=E5=8A=A0=E9=97=AD?= =?UTF-8?q?=E5=8C=85=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Model/AbstractModel.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Model/AbstractModel.php b/src/Model/AbstractModel.php index 7a7d96f..0b95efd 100644 --- a/src/Model/AbstractModel.php +++ b/src/Model/AbstractModel.php @@ -4,6 +4,7 @@ namespace YogCloud\Framework\Model; +use Closure; use Hyperf\DbConnection\Model\Model; use Hyperf\Utils\Str; @@ -158,14 +159,23 @@ public function optionWhere(array $where, array $options = []) if (! empty($where) && is_array($where)) { foreach ($where as $k => $v) { - ## 一维数组 + // 闭包 + if ($v instanceof Closure) { + $model = $model->where($v); + continue; + } + // 一维数组 if (! is_array($v)) { $model = $model->where($k, $v); continue; } - ## 二维索引数组 + // 二维索引数组 if (is_numeric($k)) { + if ($v[0] instanceof Closure) { + $model = $model->where($v[0]); + continue; + } $v[1] = mb_strtoupper($v[1]); $boolean = isset($v[3]) ? $v[3] : 'and'; if (in_array($v[1], ['=', '!=', '<', '<=', '>', '>=', 'LIKE', 'NOT LIKE'])) { @@ -178,23 +188,23 @@ public function optionWhere(array $where, array $options = []) $model = $model->whereRaw($v[0], $v[2], $boolean); } } else { - ## 二维关联数组 + // 二维关联数组 $model = $model->whereIn($k, $v); } } } - ## 排序 + // 排序 isset($options['orderByRaw']) && $model = $model->orderByRaw($options['orderByRaw']); - ## 限制集合 + // 限制集合 isset($options['skip']) && $model = $model->skip($options['skip']); isset($options['take']) && $model = $model->take($options['take']); - ## SelectRaw + // SelectRaw isset($options['selectRaw']) && $model = $model->selectRaw($options['selectRaw']); - ## With + // With isset($options['with']) && $model = $model->with($options['with']); return $model;