diff --git a/library/think/Model.php b/library/think/Model.php index 386660c4..cbf565ff 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -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 @@ -1163,9 +1177,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); } // 获取自动增长主键 diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index 901b9e61..bbb1a919 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -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); } /**