From 49b007f5428b3398a4aa4dfd1b255105e497ba6c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 7 Jul 2017 11:16:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 46 +++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 94b61bac..019cb8dc 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1161,16 +1161,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function setInc($field, $step = 1, $lazyTime = 0) { - // 删除条件 - $pk = $this->getPk(); - - if (is_string($pk) && isset($this->data[$pk])) { - $where = [$pk => $this->data[$pk]]; - } elseif (!empty($this->updateWhere)) { - $where = $this->updateWhere; - } else { - $where = null; - } + // 更新条件 + $where = $this->getWhere(); $result = $this->getQuery()->where($where)->setInc($field, $step, $lazyTime); if (true !== $result) { @@ -1190,6 +1182,23 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @throws Exception */ public function setDec($field, $step = 1, $lazyTime = 0) + { + // 更新条件 + $where = $this->getWhere(); + $result = $this->getQuery()->where($where)->setDec($field, $step, $lazyTime); + if (true !== $result) { + $this->data[$field] -= $step; + } + + return $result; + } + + /** + * 获取更新条件 + * @access protected + * @return mixed + */ + protected function getWhere() { // 删除条件 $pk = $this->getPk(); @@ -1201,13 +1210,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } else { $where = null; } - - $result = $this->getQuery()->where($where)->setDec($field, $step, $lazyTime); - if (true !== $result) { - $this->data[$field] -= $step; - } - - return $result; + return $where; } /** @@ -1335,14 +1338,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } // 删除条件 - $pk = $this->getPk(); - if (is_string($pk) && isset($this->data[$pk])) { - $where = [$pk => $this->data[$pk]]; - } elseif (!empty($this->updateWhere)) { - $where = $this->updateWhere; - } else { - $where = null; - } + $where = $this->getWhere(); // 删除当前模型数据 $result = $this->getQuery()->where($where)->delete();