改进模型

This commit is contained in:
thinkphp
2017-07-07 11:16:39 +08:00
parent d95dec72c3
commit 49b007f542

View File

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