tp5数组查询条件转为Closure查询条件
/** * 将数组查询参数改为回调查询参数 * @param $where * * @return \Closure * @internal */ public function arrayWhereToCallbackWhere($where){ // 通过键值过滤掉无效查询条件,输入参数时可将查询条件是否有效的依据放置于键中 $where = array_filter($where, function ($v, $k) { return $k && $v; }, ARRAY_FILTER_USE_BOTH); return function ($query) use ($where) { foreach ($where as $k => $v) { if (is_array($v)) { call_user_func_array([$query, 'where'], $v); } else { $query->where($v); } } }; }
输入参数可以为这样的:
User::where(arrayWhereToCallbackWhere([$this->param('username') => "find_in_set('{$id}',user_ids)"]))->select();