This commit is contained in:
2020-11-12 00:15:11 +08:00
parent 45d9e01445
commit e1c9e97b0c
+67
View File
@@ -88,6 +88,73 @@ class ActiveRecord extends BaseActiveRecord
return $this; return $this;
} }
/**
* @param string $column
* @param int $value
* @return ActiveRecord|false
* @throws Exception
*/
public function increment(string $column, int $value)
{
if (!$this->mathematics(self::INCR, [$column => $value])) {
return false;
}
$this->{$column} += $value;
return $this->refresh();
}
/**
* @param string $column
* @param int $value
* @return ActiveRecord|false
* @throws Exception
*/
public function decrement(string $column, int $value)
{
if (!$this->mathematics(self::DECR, [$column => $value])) {
return false;
}
$this->{$column} -= $value;
return $this->refresh();
}
/**
* @param array $columns
* @return ActiveRecord|false
* @throws Exception
*/
public function increments(array $columns)
{
if (!$this->mathematics(self::INCR, $columns)) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key += $attribute;
}
return $this;
}
/**
* @param array $columns
* @return ActiveRecord|false
* @throws Exception
*/
public function decrements(array $columns)
{
if (!$this->mathematics(self::DECR, $columns)) {
return false;
}
foreach ($columns as $key => $attribute) {
$this->$key -= $attribute;
}
return $this;
}
/** /**
* @param array $attributes * @param array $attributes
* @return bool|self * @return bool|self