This commit is contained in:
thinkphp
2018-12-06 15:44:35 +08:00
3 changed files with 118 additions and 88 deletions

View File

@@ -94,6 +94,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $type = [];
// 是否为更新数据
protected $isUpdate = false;
// 是否使用Replace
protected $replace = false;
// 是否强制更新所有数据
protected $force = false;
// 更新条件
@@ -1013,6 +1015,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false;
}
/**
* 新增数据是否使用Replace
* @access public
* @param bool $replace
* @return $this
*/
public function replace($replace = true)
{
$this->replace = $replace;
return $this;
}
/**
* 保存当前数据对象
* @access public
@@ -1162,9 +1176,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 检测字段
$allowFields = $this->checkAllowField(array_merge($this->auto, $this->insert));
if (!empty($allowFields)) {
$result = $this->getQuery()->strict(false)->field($allowFields)->insert($this->data, false, false, $sequence);
$result = $this->getQuery()->strict(false)->field($allowFields)->insert($this->data, $this->replace, false, $sequence);
} else {
$result = $this->getQuery()->insert($this->data, false, false, $sequence);
$result = $this->getQuery()->insert($this->data, $this->replace, false, $sequence);
}
// 获取自动增长主键

View File

@@ -160,8 +160,8 @@ class Request
/**
* Hook 方法注入
* @access public
* @param string|array $method 方法名
* @param mixed $callback callable
* @param string|array $method 方法名
* @param mixed $callback callable
* @return void
*/
public static function hook($method, $callback = null)
@@ -202,13 +202,13 @@ class Request
/**
* 创建一个URL请求
* @access public
* @param string $uri URL地址
* @param string $method 请求类型
* @param array $params 请求参数
* @param array $cookie
* @param array $files
* @param array $server
* @param string $content
* @param string $uri URL地址
* @param string $method 请求类型
* @param array $params 请求参数
* @param array $cookie
* @param array $files
* @param array $server
* @param string $content
* @return \think\Request
*/
public static function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null)
@@ -415,7 +415,7 @@ class Request
foreach (Config::get('pathinfo_fetch') as $type) {
if (!empty($_SERVER[$type])) {
$_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type], $_SERVER['SCRIPT_NAME'])) ?
substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
break;
}
}
@@ -496,8 +496,8 @@ class Request
/**
* 设置资源类型
* @access public
* @param string|array $type 资源类型名
* @param string $val 资源类型
* @param string|array $type 资源类型名
* @param string $val 资源类型
* @return void
*/
public function mimeType($type, $val = '')
@@ -512,7 +512,7 @@ class Request
/**
* 当前的请求类型
* @access public
* @param bool $method true 获取原始请求类型
* @param bool $method true 获取原始请求类型
* @return string
*/
public function method($method = false)
@@ -626,9 +626,9 @@ class Request
/**
* 获取当前请求的参数
* @access public
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function param($name = '', $default = null, $filter = '')
@@ -664,15 +664,16 @@ class Request
/**
* 设置获取路由参数
* @access public
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function route($name = '', $default = null, $filter = '')
{
if (is_array($name)) {
$this->param = [];
$this->param = [];
$this->mergeParam = false;
return $this->route = array_merge($this->route, $name);
}
return $this->input($this->route, $name, $default, $filter);
@@ -681,9 +682,9 @@ class Request
/**
* 设置获取GET参数
* @access public
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function get($name = '', $default = null, $filter = '')
@@ -693,6 +694,7 @@ class Request
}
if (is_array($name)) {
$this->param = [];
$this->mergeParam = false;
return $this->get = array_merge($this->get, $name);
}
return $this->input($this->get, $name, $default, $filter);
@@ -701,9 +703,9 @@ class Request
/**
* 设置获取POST参数
* @access public
* @param string $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @param string $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function post($name = '', $default = null, $filter = '')
@@ -717,7 +719,8 @@ class Request
}
}
if (is_array($name)) {
$this->param = [];
$this->param = [];
$this->mergeParam = false;
return $this->post = array_merge($this->post, $name);
}
return $this->input($this->post, $name, $default, $filter);
@@ -726,9 +729,9 @@ class Request
/**
* 设置获取PUT参数
* @access public
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function put($name = '', $default = null, $filter = '')
@@ -743,6 +746,7 @@ class Request
}
if (is_array($name)) {
$this->param = [];
$this->mergeParam = false;
return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name);
}
@@ -752,9 +756,9 @@ class Request
/**
* 设置获取DELETE参数
* @access public
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function delete($name = '', $default = null, $filter = '')
@@ -765,9 +769,9 @@ class Request
/**
* 设置获取PATCH参数
* @access public
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function patch($name = '', $default = null, $filter = '')
@@ -777,9 +781,9 @@ class Request
/**
* 获取request变量
* @param string $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @param string $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function request($name = '', $default = null, $filter = '')
@@ -788,7 +792,8 @@ class Request
$this->request = $_REQUEST;
}
if (is_array($name)) {
$this->param = [];
$this->param = [];
$this->mergeParam = false;
return $this->request = array_merge($this->request, $name);
}
return $this->input($this->request, $name, $default, $filter);
@@ -797,9 +802,9 @@ class Request
/**
* 获取session数据
* @access public
* @param string|array $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function session($name = '', $default = null, $filter = '')
@@ -816,9 +821,9 @@ class Request
/**
* 获取cookie参数
* @access public
* @param string|array $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function cookie($name = '', $default = null, $filter = '')
@@ -849,9 +854,9 @@ class Request
/**
* 获取server参数
* @access public
* @param string|array $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function server($name = '', $default = null, $filter = '')
@@ -927,9 +932,9 @@ class Request
/**
* 获取环境变量
* @param string|array $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @param string|array $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function env($name = '', $default = null, $filter = '')
@@ -946,8 +951,8 @@ class Request
/**
* 设置或者获取当前的Header
* @access public
* @param string|array $name header名称
* @param string $default 默认值
* @param string|array $name header名称
* @param string $default 默认值
* @return string
*/
public function header($name = '', $default = null)
@@ -985,10 +990,10 @@ class Request
/**
* 获取变量 支持过滤和默认值
* @param array $data 数据源
* @param string|false $name 字段名
* @param mixed $default 默认值
* @param string|array $filter 过滤函数
* @param array $data 数据源
* @param string|false $name 字段名
* @param mixed $default 默认值
* @param string|array $filter 过滤函数
* @return mixed
*/
public function input($data = [], $name = '', $default = null, $filter = '')
@@ -1069,9 +1074,9 @@ class Request
/**
* 递归过滤给定的值
* @param mixed $value 键值
* @param mixed $key 键名
* @param array $filters 过滤方法+默认值
* @param mixed $value 键值
* @param mixed $key 键名
* @param array $filters 过滤方法+默认值
* @return mixed
*/
private function filterValue(&$value, $key, $filters)
@@ -1156,9 +1161,9 @@ class Request
/**
* 是否存在某个请求参数
* @access public
* @param string $name 变量名
* @param string $type 变量类型
* @param bool $checkEmpty 是否检测空值
* @param string $name 变量名
* @param string $type 变量类型
* @param bool $checkEmpty 是否检测空值
* @return mixed
*/
public function has($name, $type = 'param', $checkEmpty = false)
@@ -1182,8 +1187,8 @@ class Request
/**
* 获取指定的参数
* @access public
* @param string|array $name 变量名
* @param string $type 变量类型
* @param string|array $name 变量名
* @param string $type 变量类型
* @return mixed
*/
public function only($name, $type = 'param')
@@ -1204,8 +1209,8 @@ class Request
/**
* 排除指定参数获取
* @access public
* @param string|array $name 变量名
* @param string $type 变量类型
* @param string|array $name 变量名
* @param string $type 变量类型
* @return mixed
*/
public function except($name, $type = 'param')
@@ -1247,7 +1252,7 @@ class Request
/**
* 当前是否Ajax请求
* @access public
* @param bool $ajax true 获取原始ajax请求
* @param bool $ajax true 获取原始ajax请求
* @return bool
*/
public function isAjax($ajax = false)
@@ -1257,7 +1262,7 @@ class Request
if (true === $ajax) {
return $result;
} else {
$result = $this->param(Config::get('var_ajax')) ? true : $result;
$result = $this->param(Config::get('var_ajax')) ? true : $result;
$this->mergeParam = false;
return $result;
}
@@ -1266,7 +1271,7 @@ class Request
/**
* 当前是否Pjax请求
* @access public
* @param bool $pjax true 获取原始pjax请求
* @param bool $pjax true 获取原始pjax请求
* @return bool
*/
public function isPjax($pjax = false)
@@ -1275,7 +1280,7 @@ class Request
if (true === $pjax) {
return $result;
} else {
$result = $this->param(Config::get('var_pjax')) ? true : $result;
$result = $this->param(Config::get('var_pjax')) ? true : $result;
$this->mergeParam = false;
return $result;
}
@@ -1283,13 +1288,13 @@ class Request
/**
* 获取客户端IP地址
* @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
* @param boolean $adv 是否进行高级模式获取(有可能被伪装)
* @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
* @param boolean $adv 是否进行高级模式获取(有可能被伪装)
* @return mixed
*/
public function ip($type = 0, $adv = true)
{
$type = $type ? 1 : 0;
$type = $type ? 1 : 0;
static $ip = null;
if (null !== $ip) {
return $ip[$type];
@@ -1364,7 +1369,7 @@ class Request
/**
* 当前请求的host
* @access public
* @param bool $strict true 仅仅获取HOST
* @param bool $strict true 仅仅获取HOST
* @return string
*/
public function host($strict = false)
@@ -1445,7 +1450,7 @@ class Request
/**
* 设置或者获取当前请求的调度信息
* @access public
* @param array $dispatch 调度信息
* @param array $dispatch 调度信息
* @return array
*/
public function dispatch($dispatch = null)
@@ -1565,7 +1570,7 @@ class Request
/**
* 设置当前地址的请求缓存
* @access public
* @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
* @param string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
* @param mixed $expire 缓存有效期
* @param array $except 缓存排除
* @param string $tag 缓存标签
@@ -1628,7 +1633,7 @@ class Request
throw new \think\exception\HttpResponseException($response);
} elseif (Cache::has($key)) {
list($content, $header) = Cache::get($key);
$response = Response::create($content)->header($header);
$response = Response::create($content)->header($header);
throw new \think\exception\HttpResponseException($response);
} else {
$this->cache = [$key, $expire, $tag];
@@ -1650,7 +1655,7 @@ class Request
* 设置当前请求绑定的对象实例
* @access public
* @param string|array $name 绑定的对象标识
* @param mixed $obj 绑定的对象实例
* @param mixed $obj 绑定的对象实例
* @return mixed
*/
public function bind($name, $obj = null)

View File

@@ -53,7 +53,7 @@ class MorphOne extends Relation
public function getRelation($subRelation = '', $closure = null)
{
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
call_user_func_array($closure, [& $this->query]);
}
$relationModel = $this->relation($subRelation)->find();
@@ -81,8 +81,8 @@ class MorphOne extends Relation
/**
* 根据关联条件查询当前模型
* @access public
* @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @param mixed $where 查询条件(数组或者闭包)
* @param mixed $fields 字段
* @return Query
*/
public function hasWhere($where = [], $fields = null)
@@ -179,7 +179,7 @@ class MorphOne extends Relation
{
// 预载入关联查询 支持嵌套预载入
if ($closure) {
call_user_func_array($closure, [ & $this]);
call_user_func_array($closure, [& $this]);
}
$list = $this->query->where($where)->with($subRelation)->find();
$morphKey = $this->morphKey;
@@ -198,6 +198,17 @@ class MorphOne extends Relation
* @return Model|false
*/
public function save($data)
{
$model = $this->make($data);
return $model->save() ? $model : false;
}
/**
* 创建关联对象实例
* @param array $data
* @return mixed
*/
public function make($data = [])
{
if ($data instanceof Model) {
$data = $data->getData();
@@ -205,10 +216,10 @@ class MorphOne extends Relation
// 保存关联表数据
$pk = $this->parent->getPk();
$model = new $this->model;
$data[$this->morphKey] = $this->parent->$pk;
$data[$this->morphType] = $this->type;
return $model->save($data) ? $model : false;
return new $this->model($data);
}
/**