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

View File

@@ -673,6 +673,7 @@ class Request
{ {
if (is_array($name)) { if (is_array($name)) {
$this->param = []; $this->param = [];
$this->mergeParam = false;
return $this->route = array_merge($this->route, $name); return $this->route = array_merge($this->route, $name);
} }
return $this->input($this->route, $name, $default, $filter); return $this->input($this->route, $name, $default, $filter);
@@ -693,6 +694,7 @@ class Request
} }
if (is_array($name)) { if (is_array($name)) {
$this->param = []; $this->param = [];
$this->mergeParam = false;
return $this->get = array_merge($this->get, $name); return $this->get = array_merge($this->get, $name);
} }
return $this->input($this->get, $name, $default, $filter); return $this->input($this->get, $name, $default, $filter);
@@ -718,6 +720,7 @@ class Request
} }
if (is_array($name)) { if (is_array($name)) {
$this->param = []; $this->param = [];
$this->mergeParam = false;
return $this->post = array_merge($this->post, $name); return $this->post = array_merge($this->post, $name);
} }
return $this->input($this->post, $name, $default, $filter); return $this->input($this->post, $name, $default, $filter);
@@ -743,6 +746,7 @@ class Request
} }
if (is_array($name)) { if (is_array($name)) {
$this->param = []; $this->param = [];
$this->mergeParam = false;
return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name); return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name);
} }
@@ -789,6 +793,7 @@ class Request
} }
if (is_array($name)) { if (is_array($name)) {
$this->param = []; $this->param = [];
$this->mergeParam = false;
return $this->request = array_merge($this->request, $name); return $this->request = array_merge($this->request, $name);
} }
return $this->input($this->request, $name, $default, $filter); return $this->input($this->request, $name, $default, $filter);

View File

@@ -198,6 +198,17 @@ class MorphOne extends Relation
* @return Model|false * @return Model|false
*/ */
public function save($data) 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) { if ($data instanceof Model) {
$data = $data->getData(); $data = $data->getData();
@@ -205,10 +216,10 @@ class MorphOne extends Relation
// 保存关联表数据 // 保存关联表数据
$pk = $this->parent->getPk(); $pk = $this->parent->getPk();
$model = new $this->model;
$data[$this->morphKey] = $this->parent->$pk; $data[$this->morphKey] = $this->parent->$pk;
$data[$this->morphType] = $this->type; $data[$this->morphType] = $this->type;
return $model->save($data) ? $model : false;
return new $this->model($data);
} }
/** /**