同步Library组件,支持 Model 快捷操作

This commit is contained in:
邹景立 2021-06-22 15:30:27 +08:00
parent 9b4e299239
commit bec782fd37
18 changed files with 105 additions and 106 deletions

View File

@ -594,17 +594,17 @@
}, },
{ {
"name": "topthink/think-helper", "name": "topthink/think-helper",
"version": "v3.1.4", "version": "v3.1.5",
"version_normalized": "3.1.4.0", "version_normalized": "3.1.5.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/top-think/think-helper.git", "url": "https://github.com/top-think/think-helper.git",
"reference": "c28d37743bda4a0455286ca85b17b5791d626e10" "reference": "f98e3ad44acd27ae85a4d923b1bdfd16c6d8d905"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/top-think/think-helper/zipball/c28d37743bda4a0455286ca85b17b5791d626e10", "url": "https://api.github.com/repos/top-think/think-helper/zipball/f98e3ad44acd27ae85a4d923b1bdfd16c6d8d905",
"reference": "c28d37743bda4a0455286ca85b17b5791d626e10", "reference": "f98e3ad44acd27ae85a4d923b1bdfd16c6d8d905",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -616,7 +616,7 @@
"require": { "require": {
"php": ">=7.1.0" "php": ">=7.1.0"
}, },
"time": "2019-11-08T08:01:10+00:00", "time": "2021-06-21T06:17:31+00:00",
"type": "library", "type": "library",
"installation-source": "dist", "installation-source": "dist",
"autoload": { "autoload": {
@ -638,6 +638,10 @@
} }
], ],
"description": "The ThinkPHP6 Helper Package", "description": "The ThinkPHP6 Helper Package",
"support": {
"issues": "https://github.com/top-think/think-helper/issues",
"source": "https://github.com/top-think/think-helper/tree/v3.1.5"
},
"install-path": "../topthink/think-helper" "install-path": "../topthink/think-helper"
}, },
{ {
@ -849,12 +853,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/zoujingli/ThinkLibrary.git", "url": "https://github.com/zoujingli/ThinkLibrary.git",
"reference": "283b25735511092a6c1c48e4d55b919cf1e09e30" "reference": "611043ad1974ab0926868bb44b1b2491fee13114"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/283b25735511092a6c1c48e4d55b919cf1e09e30", "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/611043ad1974ab0926868bb44b1b2491fee13114",
"reference": "283b25735511092a6c1c48e4d55b919cf1e09e30", "reference": "611043ad1974ab0926868bb44b1b2491fee13114",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -871,7 +875,7 @@
"ext-mbstring": "*", "ext-mbstring": "*",
"topthink/framework": "^6.0" "topthink/framework": "^6.0"
}, },
"time": "2021-06-18T08:57:57+00:00", "time": "2021-06-22T07:23:58+00:00",
"default-branch": true, "default-branch": true,
"type": "library", "type": "library",
"extra": { "extra": {

View File

@ -5,7 +5,7 @@
'type' => 'project', 'type' => 'project',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
'reference' => '43a8afea8902a5269d27519ebb31caa0705e2c82', 'reference' => '9b4e299239c063b71f431d974c0754fadb9a6624',
'name' => 'zoujingli/thinkadmin', 'name' => 'zoujingli/thinkadmin',
'dev' => false, 'dev' => false,
), ),
@ -92,12 +92,12 @@
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'topthink/think-helper' => array( 'topthink/think-helper' => array(
'pretty_version' => 'v3.1.4', 'pretty_version' => 'v3.1.5',
'version' => '3.1.4.0', 'version' => '3.1.5.0',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../topthink/think-helper', 'install_path' => __DIR__ . '/../topthink/think-helper',
'aliases' => array(), 'aliases' => array(),
'reference' => 'c28d37743bda4a0455286ca85b17b5791d626e10', 'reference' => 'f98e3ad44acd27ae85a4d923b1bdfd16c6d8d905',
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'topthink/think-orm' => array( 'topthink/think-orm' => array(
@ -144,7 +144,7 @@
'aliases' => array( 'aliases' => array(
0 => '9999999-dev', 0 => '9999999-dev',
), ),
'reference' => '283b25735511092a6c1c48e4d55b919cf1e09e30', 'reference' => '611043ad1974ab0926868bb44b1b2491fee13114',
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'zoujingli/thinkadmin' => array( 'zoujingli/thinkadmin' => array(
@ -153,7 +153,7 @@
'type' => 'project', 'type' => 'project',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
'reference' => '43a8afea8902a5269d27519ebb31caa0705e2c82', 'reference' => '9b4e299239c063b71f431d974c0754fadb9a6624',
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'zoujingli/wechat-developer' => array( 'zoujingli/wechat-developer' => array(

2
vendor/services.php vendored
View File

@ -1,5 +1,5 @@
<?php <?php
// This file is automatically generated at:2021-06-18 16:59:49 // This file is automatically generated at:2021-06-22 15:29:50
declare (strict_types = 1); declare (strict_types = 1);
return array ( return array (
0 => 'think\\admin\\Library', 0 => 'think\\admin\\Library',

View File

@ -242,15 +242,17 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
* @access public * @access public
* @param mixed $value 元素 * @param mixed $value 元素
* @param string $key KEY * @param string $key KEY
* @return void * @return $this
*/ */
public function push($value, string $key = null): void public function push($value, string $key = null)
{ {
if (is_null($key)) { if (is_null($key)) {
$this->items[] = $value; $this->items[] = $value;
} else { } else {
$this->items[$key] = $value; $this->items[$key] = $value;
} }
return $this;
} }
/** /**
@ -277,15 +279,17 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
* @access public * @access public
* @param mixed $value 元素 * @param mixed $value 元素
* @param string $key KEY * @param string $key KEY
* @return void * @return $this
*/ */
public function unshift($value, string $key = null): void public function unshift($value, string $key = null)
{ {
if (is_null($key)) { if (is_null($key)) {
array_unshift($this->items, $value); array_unshift($this->items, $value);
} else { } else {
$this->items = [$key => $value] + $this->items; $this->items = [$key => $value] + $this->items;
} }
return $this;
} }
/** /**
@ -477,7 +481,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
* @param string|null $indexKey 作为索引值的列 * @param string|null $indexKey 作为索引值的列
* @return array * @return array
*/ */
public function column(?string $columnKey, string $indexKey = null) public function column( ? string $columnKey, string $indexKey = null)
{ {
return array_column($this->items, $columnKey, $indexKey); return array_column($this->items, $columnKey, $indexKey);
} }
@ -515,7 +519,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
$fieldA = $a[$field] ?? null; $fieldA = $a[$field] ?? null;
$fieldB = $b[$field] ?? null; $fieldB = $b[$field] ?? null;
return 'desc' == strtolower($order) ? $fieldB > $fieldA : $fieldA > $fieldB; return 'desc' == strtolower($order) ? intval($fieldB > $fieldA) : intval($fieldA > $fieldB);
}); });
} }
@ -535,7 +539,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
} }
/** /**
* 获取第一个单元数据 * 获取第一个单元数据
* *
* @access public * @access public
* @param callable|null $callback * @param callable|null $callback
@ -623,7 +627,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
* @param integer $options json参数 * @param integer $options json参数
* @return string * @return string
*/ */
public function toJson(int $options = JSON_UNESCAPED_UNICODE): string public function toJson(int $options = JSON_UNESCAPED_UNICODE) : string
{ {
return json_encode($this->toArray(), $options); return json_encode($this->toArray(), $options);
} }

View File

@ -1,8 +1,7 @@
.git .git
.svn .svn
.idea .idea
!.gitignore
!composer.json
/composer.lock /composer.lock
/vendor /vendor
/test.php !.gitignore
!composer.json

View File

@ -32,6 +32,7 @@ use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
use think\db\Query; use think\db\Query;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
use think\Model;
use think\Request; use think\Request;
/** /**
@ -183,7 +184,7 @@ abstract class Controller extends stdClass
/** /**
* 快捷查询逻辑器 * 快捷查询逻辑器
* @param string|Query $dbQuery * @param Model|Query|string $dbQuery
* @param array|string|null $input * @param array|string|null $input
* @return QueryHelper * @return QueryHelper
*/ */
@ -194,7 +195,7 @@ abstract class Controller extends stdClass
/** /**
* 快捷分页逻辑器 * 快捷分页逻辑器
* @param string|Query $dbQuery * @param Model|Query|string $dbQuery
* @param boolean $page 是否启用分页 * @param boolean $page 是否启用分页
* @param boolean $display 是否渲染模板 * @param boolean $display 是否渲染模板
* @param boolean|integer $total 集合分页记录数 * @param boolean|integer $total 集合分页记录数
@ -212,7 +213,7 @@ abstract class Controller extends stdClass
/** /**
* 快捷表单逻辑器 * 快捷表单逻辑器
* @param string|Query $dbQuery * @param Model|Query|string $dbQuery
* @param string $template 模板名称 * @param string $template 模板名称
* @param string $field 指定数据对象主键 * @param string $field 指定数据对象主键
* @param array $where 额外更新条件 * @param array $where 额外更新条件
@ -240,7 +241,7 @@ abstract class Controller extends stdClass
/** /**
* 快捷更新逻辑器 * 快捷更新逻辑器
* @param string|Query $dbQuery * @param Model|Query|string $dbQuery
* @param array $data 表单扩展数据 * @param array $data 表单扩展数据
* @param string $field 数据对象主键 * @param string $field 数据对象主键
* @param array $where 额外更新条件 * @param array $where 额外更新条件
@ -254,7 +255,7 @@ abstract class Controller extends stdClass
/** /**
* 快捷删除逻辑器 * 快捷删除逻辑器
* @param string|Query $dbQuery * @param Model|Query|string $dbQuery
* @param string $field 数据对象主键 * @param string $field 数据对象主键
* @param array $where 额外更新条件 * @param array $where 额外更新条件
* @return boolean|null * @return boolean|null

View File

@ -42,15 +42,6 @@ class Exception extends \Exception
parent::__construct($message, $code); parent::__construct($message, $code);
} }
/**
* 设置异常停止数据
* @param mixed $data
*/
public function setData($data)
{
$this->data = $data;
}
/** /**
* 获取异常停止数据 * 获取异常停止数据
* @return mixed * @return mixed
@ -60,4 +51,13 @@ class Exception extends \Exception
return $this->data; return $this->data;
} }
/**
* 设置异常停止数据
* @param mixed $data
*/
public function setData($data)
{
$this->data = $data;
}
} }

View File

@ -19,8 +19,9 @@ namespace think\admin;
use think\App; use think\App;
use think\Container; use think\Container;
use think\Db; use think\db\BaseQuery;
use think\db\Query; use think\db\Query;
use think\Model;
/** /**
* 控制器挂件 * 控制器挂件
@ -36,8 +37,14 @@ abstract class Helper
public $app; public $app;
/** /**
* 数据库实例 * 数据模型实例
* @var Db|Query * @var Model
*/
public $model;
/**
* 数据查询实例
* @var Query
*/ */
public $query; public $query;
@ -60,12 +67,19 @@ abstract class Helper
/** /**
* 获取数据库对象 * 获取数据库对象
* @param string|Db|Query $dbQuery * @param Model|Query|string $dbQuery
* @return Db|Query * @return Query|mixed
*/ */
protected function buildQuery($dbQuery) protected function buildQuery($dbQuery)
{ {
return is_string($dbQuery) ? $this->app->db->name($dbQuery) : $dbQuery; if (is_string($dbQuery)) {
$this->query = $this->app->db->name($dbQuery);
} elseif ($dbQuery instanceof \think\Model) {
$this->query = $dbQuery->db();
} elseif ($dbQuery instanceof BaseQuery) {
$this->query = $dbQuery;
}
return $this->query;
} }
/** /**

View File

@ -45,18 +45,16 @@ abstract class Service
/** /**
* 初始化服务 * 初始化服务
* @return static
*/ */
protected function initialize() protected function initialize()
{ {
return $this;
} }
/** /**
* 静态实例对象 * 静态实例对象
* @param array $var 实例参数 * @param array $var 实例参数
* @param boolean $new 创建新实例 * @param boolean $new 创建新实例
* @return static * @return static|mixed
*/ */
public static function instance(array $var = [], bool $new = false) public static function instance(array $var = [], bool $new = false)
{ {

View File

@ -23,6 +23,7 @@ use think\db\exception\DataNotFoundException;
use think\db\exception\DbException; use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
use think\db\Query; use think\db\Query;
use think\Model;
if (!function_exists('p')) { if (!function_exists('p')) {
/** /**
@ -264,7 +265,7 @@ if (!function_exists('http_post')) {
if (!function_exists('data_save')) { if (!function_exists('data_save')) {
/** /**
* 数据增量保存 * 数据增量保存
* @param Query|string $dbQuery * @param Model|Query|string $dbQuery
* @param array $data 需要保存或更新的数据 * @param array $data 需要保存或更新的数据
* @param string $key 条件主键限制 * @param string $key 条件主键限制
* @param array $where 其它的where条件 * @param array $where 其它的where条件

View File

@ -73,7 +73,7 @@ class JsonRpcServer
$response = ['jsonrpc' => '2.0', 'id' => '0', 'result' => null, 'error' => $error]; $response = ['jsonrpc' => '2.0', 'id' => '0', 'result' => null, 'error' => $error];
} elseif (!isset($request['id']) || !isset($request['method']) || !isset($request['params'])) { } elseif (!isset($request['id']) || !isset($request['method']) || !isset($request['params'])) {
$error = ['code' => '-32600', 'message' => '无效的请求', 'meaning' => '发送的JSON不是一个有效的请求对象']; $error = ['code' => '-32600', 'message' => '无效的请求', 'meaning' => '发送的JSON不是一个有效的请求对象'];
$response = ['jsonrpc' => '2.0', 'id' => $request['id'], 'result' => null, 'error' => $error]; $response = ['jsonrpc' => '2.0', 'id' => $request['id'] ?? '0', 'result' => null, 'error' => $error];
} else try { } else try {
// Executes the task on local object // Executes the task on local object
if (method_exists($object, $request['method'])) { if (method_exists($object, $request['method'])) {

View File

@ -18,9 +18,9 @@ declare (strict_types=1);
namespace think\admin\helper; namespace think\admin\helper;
use think\admin\Helper; use think\admin\Helper;
use think\Db;
use think\db\exception\DbException; use think\db\exception\DbException;
use think\db\Query; use think\db\Query;
use think\Model;
/** /**
* 通用删除管理器 * 通用删除管理器
@ -31,7 +31,7 @@ class DeleteHelper extends Helper
{ {
/** /**
* 逻辑器初始化 * 逻辑器初始化
* @param string|Db|Query $dbQuery * @param Model|Query|string $dbQuery
* @param string $field 操作数据主键 * @param string $field 操作数据主键
* @param array $where 额外更新条件 * @param array $where 额外更新条件
* @return boolean|null * @return boolean|null
@ -45,7 +45,7 @@ class DeleteHelper extends Helper
// 查询限制处理 // 查询限制处理
if (!empty($where)) $query->where($where); if (!empty($where)) $query->where($where);
if (!isset($where[$field]) && is_string($value)) { if (!isset($where[$field]) && is_string($value)) {
$query->whereIn($field, explode(',', $value)); $query->whereIn($field, str2arr($value));
} }
// 前置回调处理 // 前置回调处理
if (false === $this->class->callback('_delete_filter', $query, $where)) { if (false === $this->class->callback('_delete_filter', $query, $where)) {

View File

@ -18,11 +18,11 @@ declare (strict_types=1);
namespace think\admin\helper; namespace think\admin\helper;
use think\admin\Helper; use think\admin\Helper;
use think\Db;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
use think\db\exception\DbException; use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
use think\db\Query; use think\db\Query;
use think\Model;
/** /**
* 表单视图管理器 * 表单视图管理器
@ -34,7 +34,7 @@ class FormHelper extends Helper
/** /**
* 逻辑器初始化 * 逻辑器初始化
* @param string|Db|Query $dbQuery * @param Model|Query|string $dbQuery
* @param string $template 模板名称 * @param string $template 模板名称
* @param string $field 指定数据主键 * @param string $field 指定数据主键
* @param array $where 额外更新条件 * @param array $where 额外更新条件
@ -48,7 +48,7 @@ class FormHelper extends Helper
{ {
$query = $this->buildQuery($dbQuery); $query = $this->buildQuery($dbQuery);
$field = $field ?: ($query->getPk() ?: 'id'); $field = $field ?: ($query->getPk() ?: 'id');
$value = input($field, $data[$field] ?? null); $value = $data[$field] ?? input($field);
if ($this->app->request->isGet()) { if ($this->app->request->isGet()) {
if ($value !== null) { if ($value !== null) {
$find = $query->where([$field => $value])->where($where)->find(); $find = $query->where([$field => $value])->where($where)->find();

View File

@ -22,6 +22,7 @@ use think\db\exception\DataNotFoundException;
use think\db\exception\DbException; use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
use think\db\Query; use think\db\Query;
use think\Model;
/** /**
* 列表处理管理器 * 列表处理管理器
@ -33,7 +34,7 @@ class PageHelper extends Helper
/** /**
* 逻辑器初始化 * 逻辑器初始化
* @param string|Query $dbQuery * @param Model|Query|string $dbQuery
* @param boolean $page 是否启用分页 * @param boolean $page 是否启用分页
* @param boolean $display 是否渲染模板 * @param boolean $display 是否渲染模板
* @param boolean|integer $total 集合分页记录数 * @param boolean|integer $total 集合分页记录数
@ -48,9 +49,18 @@ class PageHelper extends Helper
{ {
$this->query = $this->buildQuery($dbQuery); $this->query = $this->buildQuery($dbQuery);
// 数据列表排序自动处理 // 数据列表排序自动处理
if ($this->app->request->isPost()) $this->_sortAction(); if ($this->app->request->isPost() && $this->app->request->post('action') === 'sort') {
// 列表设置默认排序处理 if (in_array('sort', $this->query->getTableFields())) {
if (!$this->query->getOptions('order')) $this->_orderAction(); if ($this->app->request->has($pk = $this->query->getPk() ?: 'id', 'post')) {
$map = [$pk => $this->app->request->post($pk, 0)];
$data = ['sort' => intval($this->app->request->post('sort', 0))];
if ($this->app->db->table($this->query->getTable())->where($map)->update($data) !== false) {
$this->class->success(lang('think_library_sort_success'), '');
}
}
}
$this->class->error(lang('think_library_sort_error'));
}
// 列表分页及结果集处理 // 列表分页及结果集处理
if ($page) { if ($page) {
if (empty($limit)) { if (empty($limit)) {
@ -92,40 +102,4 @@ class PageHelper extends Helper
} }
return $result; return $result;
} }
/**
* 执行列表排序操作
* POST 提交 {action:sort,PK:$PK,SORT:$SORT}
* @throws DbException
*/
private function _sortAction()
{
if ($this->app->request->post('action') === 'sort') {
if (method_exists($this->query, 'getTableFields') && in_array('sort', $this->query->getTableFields())) {
$pk = $this->query->getPk() ?: 'id';
if ($this->app->request->has($pk, 'post')) {
$map = [$pk => $this->app->request->post($pk, 0)];
$data = ['sort' => intval($this->app->request->post('sort', 0))];
if ($this->app->db->table($this->query->getTable())->where($map)->update($data) !== false) {
$this->class->success(lang('think_library_sort_success'), '');
}
}
}
$this->class->error(lang('think_library_sort_error'));
}
}
/**
* 列表默认排序处理
* 未配置排序规则时自动按SORT排序
*/
private function _orderAction()
{
if (method_exists($this->query, 'getTableFields')) {
if (in_array('sort', $this->query->getTableFields())) {
$this->query->order('sort desc');
}
}
}
} }

View File

@ -18,11 +18,11 @@ declare (strict_types=1);
namespace think\admin\helper; namespace think\admin\helper;
use think\admin\Helper; use think\admin\Helper;
use think\Db;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
use think\db\exception\DbException; use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
use think\db\Query; use think\db\Query;
use think\Model;
/** /**
* 搜索条件处理器 * 搜索条件处理器
@ -41,16 +41,16 @@ class QueryHelper extends Helper
/** /**
* 获取当前Db操作对象 * 获取当前Db操作对象
* @return Db|Query * @return Query
*/ */
public function db() public function db(): Query
{ {
return $this->query; return $this->query;
} }
/** /**
* 逻辑器初始化 * 逻辑器初始化
* @param string|Query|Db $dbQuery * @param Model|Query|string $dbQuery
* @param array|string|null $input 输入数据 * @param array|string|null $input 输入数据
* @return $this * @return $this
*/ */

View File

@ -20,6 +20,7 @@ namespace think\admin\helper;
use think\admin\Helper; use think\admin\Helper;
use think\db\exception\DbException; use think\db\exception\DbException;
use think\db\Query; use think\db\Query;
use think\Model;
/** /**
* 数据更新管理器 * 数据更新管理器
@ -31,7 +32,7 @@ class SaveHelper extends Helper
/** /**
* 逻辑器初始化 * 逻辑器初始化
* @param Query|string $dbQuery * @param Model|Query|string $dbQuery
* @param array $data 表单扩展数据 * @param array $data 表单扩展数据
* @param string $field 数据对象主键 * @param string $field 数据对象主键
* @param array $where 额外更新条件 * @param array $where 额外更新条件

View File

@ -58,7 +58,7 @@ class MenuService extends Service
public function getTree(): array public function getTree(): array
{ {
$query = $this->app->db->name('SystemMenu'); $query = $this->app->db->name('SystemMenu');
$query->where(['status' => '1'])->order('sort desc,id asc'); $query->where(['status' => 1])->order('sort desc,id asc');
return $this->_buildData(DataExtend::arr2tree($query->select()->toArray())); return $this->_buildData(DataExtend::arr2tree($query->select()->toArray()));
} }

View File

@ -25,6 +25,7 @@ use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
use think\db\Query; use think\db\Query;
use think\helper\Str; use think\helper\Str;
use think\Model;
/** /**
* 系统参数管理服务 * 系统参数管理服务
@ -106,7 +107,7 @@ class SystemService extends Service
/** /**
* 数据增量保存 * 数据增量保存
* @param Query|string $query 数据查询对象 * @param Model|Query|string $query 数据查询对象
* @param array $data 需要保存的数据 * @param array $data 需要保存的数据
* @param string $key 更新条件查询主键 * @param string $key 更新条件查询主键
* @param array $map 额外更新查询条件 * @param array $map 额外更新查询条件
@ -117,6 +118,7 @@ class SystemService extends Service
*/ */
public function save($query, array $data, string $key = 'id', array $map = []) public function save($query, array $data, string $key = 'id', array $map = [])
{ {
if ($query instanceof Model) $query = $query->db();
if (is_string($query)) $query = $this->app->db->name($query); if (is_string($query)) $query = $this->app->db->name($query);
[$query, $value] = [$query->master()->strict(false)->where($map), $data[$key] ?? null]; [$query, $value] = [$query->master()->strict(false)->where($map), $data[$key] ?? null];
if (empty($map[$key])) if (is_string($value) && strpos($value, ',') !== false) { if (empty($map[$key])) if (is_string($value) && strpos($value, ',') !== false) {
@ -125,6 +127,7 @@ class SystemService extends Service
$query->where([$key => $value]); $query->where([$key => $value]);
} }
if (($info = (clone $query)->find()) && !empty($info)) { if (($info = (clone $query)->find()) && !empty($info)) {
if ($info instanceof Model) $info = $info->toArray();
$query->update($data); $query->update($data);
return $info[$key] ?? true; return $info[$key] ?? true;
} else { } else {