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();