From dcc1f7b15e8f45637d5af450c681c4d14966ff8f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 17 Jun 2016 11:55:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E7=9A=84save?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20=E4=BF=AE=E6=AD=A3Merge=E7=B1=BB=E7=9A=84s?= =?UTF-8?q?ave=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81=20where=E5=8F=82?= =?UTF-8?q?=E6=95=B0=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 | 11 +++++------ library/think/model/Merge.php | 13 ++++++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 92b49037..31caed62 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -586,7 +586,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } $result = $this->db()->where($where)->update($data); - + // 清空change + $this->change = []; // 更新回调 $this->trigger('after_update', $this); } else { @@ -613,16 +614,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } $result = $insertId; } + // 标记为更新 + $this->isUpdate = true; // 新增回调 $this->trigger('after_insert', $this); } // 写入回调 $this->trigger('after_write', $this); - - // 标记为更新 - $this->isUpdate = true; - // 清空change - $this->change = []; + return $result; } diff --git a/library/think/model/Merge.php b/library/think/model/Merge.php index 3da4fc07..69ef91c5 100644 --- a/library/think/model/Merge.php +++ b/library/think/model/Merge.php @@ -183,10 +183,21 @@ class Merge extends Model return false; } + if (empty($where) && !empty($this->updateWhere)) { + $where = $this->updateWhere; + } + + if (!empty($where)) { + $pk = $this->getPk(); + if (is_string($pk) && isset($data[$pk])) { + unset($data[$pk]); + } + } + // 处理模型数据 $data = $this->parseData($this->name, $this->data); // 写入主表数据 - $result = $db->strict(false)->update($data); + $result = $db->strict(false)->where($where)->update($data); // 写入附表数据 foreach (static::$relationModel as $key => $model) {