diff --git a/library/think/Model.php b/library/think/Model.php index f28e5031..100e4cd6 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -929,18 +929,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } // 获取有更新的数据 - $data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) { - return is_object($a) || $a != $b ? 1 : 0; - }); - - if (!empty($this->readonly)) { - // 只读字段不允许更新 - foreach ($this->readonly as $key => $field) { - if (isset($data[$field])) { - unset($data[$field]); - } - } - } + $data = $this->getChangedData(); if (empty($data) || (count($data) == 1 && is_string($pk) && isset($data[$pk]))) { // 没有更新 @@ -1049,6 +1038,29 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $result; } + /** + * 获取变化的数据 并排除只读数据 + * @access public + * @return array + */ + public function getChangedData() + { + $data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) { + return is_object($a) || $a != $b ? 1 : 0; + }); + + if (!empty($this->readonly)) { + // 只读字段不允许更新 + foreach ($this->readonly as $key => $field) { + if (isset($data[$field])) { + unset($data[$field]); + } + } + } + + return $data; + } + /** * 保存多个数据到当前数据对象 * @access public