From b5beaeded34b40132effdd5bb2249d3088d65849 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 5 May 2016 12:34:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20=E6=8C=89=E7=85=A7=E6=9F=A5=E8=AF=A2=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=20=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 | 19 ++++++++++++++++++- library/think/db/Query.php | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 83aad670..1e5d4400 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -64,6 +64,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $type = []; // 是否为更新数据 protected $isUpdate = false; + // 更新条件 + protected $updateWhere; // 当前执行的关联对象 protected $relation; @@ -287,6 +289,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } } + 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]); + } + } + $result = self::db()->where($where)->update($data); // 更新回调 @@ -353,11 +366,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 是否为更新数据 * @access public * @param bool $update + * @param mixed $where * @return $this */ - public function isUpdate($update = true) + public function isUpdate($update = true, $where = null) { $this->isUpdate = $update; + if (!empty($where)) { + $this->updateWhere = $where; + } return $this; } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 6c7e4973..c7a5fc78 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1337,7 +1337,7 @@ class Query if (!empty($options['model'])) { // 返回模型对象 $data = new $options['model']($data); - $data->isUpdate(true); + $data->isUpdate(true, $options['where']['AND']); // 关联查询 if (!empty($options['relation'])) { $data->relationQuery($options['relation']);