Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions core/classes/Database/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,33 +386,67 @@ public function update(string $table, $where, array $fields): bool
}

/**
* Increment a numeric column value by 1.
* Increment a numeric column value by 1 or array of data in "column => value" format to increment.
*
* @param string $table The table to use.
* @param int $id The id of the row to increment a column in.
* @param string $field The field to increment.
* @return bool Whether an error occurred or not.
* @param string $table The table to use.
* @param int $id The id of the row to increment a column in.
* @param string|array $fields Array of data to in "column => value" format to increment.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, if $fields is in the format ['column_one', 'column_two'], then we could default the value to 1?

* @return bool Whether an error occurred or not.
*/
public function increment(string $table, int $id, string $field): bool
public function increment(string $table, int $id, $fields): bool
{
$table = $this->_prefix . $table;

return !$this->query("UPDATE {$table} SET {$field} = {$field} + 1 WHERE id = ?", [$id])->error();
if (!is_array($fields)) {
return !$this->query("UPDATE {$table} SET {$fields} = {$fields} + 1 WHERE id = ?", [$id])->error();
} else {
$set = '';
$x = 1;

foreach (array_keys($fields) as $column) {
$set .= "`{$column}` = `{$column}` + ?";
if ($x < count($fields)) {
$set .= ', ';
}
$x++;
}
}

$sql = "UPDATE {$table} SET {$set} WHERE id = ?";

return !$this->query($sql, array_merge($fields, [$id]))->error();
}

/**
* Decrement a numeric column value by 1.
* Decrement a numeric column value by 1 or array of data in "column => value" format to decrement.
*
* @param string $table The table to use.
* @param int $id The id of the row to decrement a column in.
* @param string $field The field to increment.
* @return bool Whether an error occurred or not.
* @param string $table The table to use.
* @param int $id The id of the row to decrement a column in.
* @param string|array $fields Array of data to in "column => value" format to decrement.
* @return bool Whether an error occurred or not.
*/
public function decrement(string $table, int $id, string $field): bool
public function decrement(string $table, int $id, $fields): bool
{
$table = $this->_prefix . $table;

return !$this->query("UPDATE {$table} SET {$field} = {$field} - 1 WHERE id = ?", [$id])->error();
if (!is_array($fields)) {
return !$this->query("UPDATE {$table} SET {$fields} = {$fields} - 1 WHERE id = ?", [$id])->error();
} else {
$set = '';
$x = 1;

foreach (array_keys($fields) as $column) {
$set .= "`{$column}` = `{$column}` - ?";
if ($x < count($fields)) {
$set .= ', ';
}
$x++;
}
}

$sql = "UPDATE {$table} SET {$set} WHERE id = ?";

return !$this->query($sql, array_merge($fields, [$id]))->error();
}

/**
Expand Down