From a45586ad029024af06ae2620978f572e545fde6f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 20 May 2017 14:47:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8B=E7=B1=BB?= =?UTF-8?q?=E7=9A=84save=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index e03970e1..55e7da8e 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -952,15 +952,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } $pk = $this->getPk(); if ($this->isUpdate) { - // 获取有更新的数据 - $data = $this->getChangedData(); - // 检测字段 - $this->checkAllowField($data); + $this->checkAllowField($this->data, array_merge($this->auto, $this->update)); // 自动更新 $this->autoCompleteData($this->update); + // 获取有更新的数据 + $data = $this->getChangedData(); + // 事件回调 if (false === $this->trigger('before_update', $this)) { return false; @@ -1009,7 +1009,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } else { // 检测字段 - $this->checkAllowField($this->data); + $this->checkAllowField($this->data, array_merge($this->auto, $this->insert)); // 自动写入 $this->autoCompleteData($this->insert); @@ -1061,14 +1061,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $result; } - protected function checkAllowField(&$data) + protected function checkAllowField(&$data, $auto = []) { if (!empty($this->field)) { if (true === $this->field) { $this->field = $this->getQuery()->getTableInfo('', 'fields'); + $field = $this->field; + } else { + $field = array_merge($this->field, $auto); } + foreach ($data as $key => $val) { - if (!in_array($key, $this->field)) { + if (!in_array($key, $field)) { unset($data[$key]); } }