改进数据更新检查

This commit is contained in:
thinkphp
2017-04-13 16:21:45 +08:00
parent e9b0abd7f3
commit 79b5d43023
2 changed files with 27 additions and 22 deletions

View File

@@ -66,8 +66,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $append = []; protected $append = [];
// 数据信息 // 数据信息
protected $data = []; protected $data = [];
// 记录改变字段 // 原始数据
protected $change = []; protected $origin = [];
// 保存自动完成列表 // 保存自动完成列表
protected $auto = []; protected $auto = [];
@@ -121,6 +121,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} else { } else {
$this->data = $data; $this->data = $data;
} }
// 记录原始数据
$this->origin = $this->data;
// 当前类名 // 当前类名
$this->class = get_called_class(); $this->class = get_called_class();
@@ -345,16 +347,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
} }
// 标记字段更改
if (!isset($this->data[$name])) {
$this->change[] = $name;
} elseif (is_scalar($value) && is_scalar($this->data[$name]) && 0 !== strcmp($this->data[$name], $value)) {
$this->change[] = $name;
} elseif (!is_object($value) && $value != $this->data[$name]) {
$this->change[] = $name;
}
// 设置数据对象属性 // 设置数据对象属性
$this->data[$name] = $value; $this->data[$name] = $value;
return $this; return $this;
} }
@@ -933,13 +928,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false; return false;
} }
// 去除没有更新的字段 // 获取有更新的数据
$data = []; $data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) {
foreach ($this->data as $key => $val) { return $a === $b ? 0 : 1;
if (in_array($key, $this->change) || $this->isPk($key)) { });
$data[$key] = $val;
}
}
if (!empty($this->readonly)) { if (!empty($this->readonly)) {
// 只读字段不允许更新 // 只读字段不允许更新
@@ -962,6 +954,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$where = $this->updateWhere; $where = $this->updateWhere;
} }
// 保留主键数据
foreach ($this->data as $key => $val) {
if ($this->isPk($key)) {
$data[$key] = $val;
}
}
if (is_string($pk) && isset($data[$pk])) { if (is_string($pk) && isset($data[$pk])) {
if (!isset($where[$pk])) { if (!isset($where[$pk])) {
unset($where); unset($where);
@@ -1044,8 +1043,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 写入回调 // 写入回调
$this->trigger('after_write', $this); $this->trigger('after_write', $this);
// 清空change // 重新记录原始数据
$this->change = []; $this->origin = $this->data;
return $result; return $result;
} }
@@ -1152,9 +1151,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$field = $value; $field = $value;
$value = null; $value = null;
} }
if (!in_array($field, $this->change)) { if (!isset($this->data[$field])) {
$this->setAttr($field, !is_null($value) ? $value : (isset($this->data[$field]) ? $this->data[$field] : $value)); $default = null;
} elseif (isset($this->origin[$field]) && $this->data[$field] === $this->origin[$field]) {
$default = $this->data[$field];
} }
$this->setAttr($field, !is_null($value) ? $value : $default);
} }
} }
@@ -1194,6 +1197,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
$this->trigger('after_delete', $this); $this->trigger('after_delete', $this);
// 清空原始数据
$this->origin = [];
return $result; return $result;
} }

View File

@@ -60,7 +60,6 @@ trait SoftDelete
$name = $this->getDeleteTimeField(); $name = $this->getDeleteTimeField();
if (!$force) { if (!$force) {
// 软删除 // 软删除
$this->change[] = $name;
$this->data[$name] = $this->autoWriteTimestamp($name); $this->data[$name] = $this->autoWriteTimestamp($name);
$result = $this->isUpdate()->save(); $result = $this->isUpdate()->save();
} else { } else {