Skip to content

Commit

Permalink
faet: where增加闭包查询
Browse files Browse the repository at this point in the history
  • Loading branch information
cexll committed Apr 26, 2022
1 parent 73b3f6f commit 5657348
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace YogCloud\Framework\Model;

use Closure;
use Hyperf\DbConnection\Model\Model;
use Hyperf\Utils\Str;

Expand Down Expand Up @@ -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'])) {
Expand All @@ -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;
Expand Down

0 comments on commit 5657348

Please sign in to comment.