改进Model类的save方法

This commit is contained in:
thinkphp
2016-05-04 18:50:03 +08:00
parent f87642c6ea
commit 3774e3451a

View File

@@ -263,13 +263,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
// 去除没有更新的字段 // 去除没有更新的字段
$data = [];
foreach ($this->data as $key => $val) { foreach ($this->data as $key => $val) {
if (!in_array($key, $this->change) && !$this->isPk($key)) { if (in_array($key, $this->change) || $this->isPk($key)) {
unset($this->data[$key]); $data[$key] = $val;
} }
} }
$result = self::db()->where($where)->update($this->data); $result = self::db()->where($where)->update($data);
// 更新回调 // 更新回调
$this->trigger('after_update', $this); $this->trigger('after_update', $this);