From 34e2317b7c4b0fab46f0d6e18159761df5bdcc08 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 23 Jan 2016 11:43:56 +0800 Subject: [PATCH] =?UTF-8?q?model=E7=B1=BB=E5=A2=9E=E5=8A=A0=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=95=B0=E6=8D=AE=E5=89=AF=E6=9C=AC=20=E5=9C=A8?= =?UTF-8?q?=E5=86=99=E5=85=A5=E6=95=B0=E6=8D=AE=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E5=8F=98=E5=8C=96=E5=AD=97=E6=AE=B5=20?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=8F=98=E5=8C=96=E7=9A=84=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E4=B8=8D=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/library/think/Model.php b/library/think/Model.php index 21deae1a..912d6dbb 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -44,6 +44,8 @@ class Model protected $fields = []; // 数据信息 protected $data = []; + // 数据副本 + protected $duplicate = []; // 查询表达式参数 protected $options = []; // 命名范围定义 @@ -181,6 +183,29 @@ class Model */ protected function _write_data($data) { + if (!empty($this->duplicate)) { + // 存在数据副本 + $data = array_diff_assoc($data, $this->duplicate); + if (empty($data)) { + // 没有数据变化 + return []; + } + // 保留主键信息 + $pk = $this->getPk(); + if (is_array($pk)) { + foreach ($pk as $key) { + if (isset($this->duplicate[$key])) { + $data[$key] = $this->duplicate[$key]; + } + } + } else { + if (isset($this->duplicate[$pk])) { + $data[$pk] = $this->duplicate[$pk]; + } + } + // 重置副本 + $this->duplicate = []; + } // 检查字段映射 if (!empty($this->map)) { foreach ($this->map as $key => $val) { @@ -885,6 +910,8 @@ class Model } // 数据对象赋值 $this->data = $data; + // 数据副本 + $this->duplicate = $data; return $this->data; }