From ea36fdfbe992ea36ee1aeb94fb89e0fa0c4cdd68 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 21 Nov 2017 16:46:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=B1=BB=E5=A2=9E=E5=8A=A0fo?= =?UTF-8?q?rce=E6=96=B9=E6=B3=95=E8=AE=BE=E7=BD=AE=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=BC=BA=E5=88=B6=E6=9B=B4=E6=96=B0=E6=89=80=E6=9C=89=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 9622858a..d7dc4536 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -94,6 +94,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $type = []; // 是否为更新数据 protected $isUpdate = false; + // 是否强制更新所有数据 + protected $force = false; // 更新条件 protected $updateWhere; // 验证失败是否抛出异常 @@ -350,6 +352,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $this; } + /** + * 更新是否强制写入数据 而不做比较 + * @access public + * @param bool $force + * @return $this + */ + public function force($force = true) + { + $this->force = $force; + return $this; + } + /** * 修改器 设置数据对象值 * @access public @@ -1186,12 +1200,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function getChangedData() { - $data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) { - if ((empty($a) || empty($b)) && $a !== $b) { - return 1; - } - return is_object($a) || $a != $b ? 1 : 0; - }); + if ($this->force) { + $data = $this->data; + } else { + $data = array_udiff_assoc($this->data, $this->origin, function ($a, $b) { + if ((empty($a) || empty($b)) && $a !== $b) { + return 1; + } + return is_object($a) || $a != $b ? 1 : 0; + }); + } if (!empty($this->readonly)) { // 只读字段不允许更新