Skip to main content
Version: Fegg 5.x (next)

Pdo::setIncrement

Add to a numeric column in an UPDATE

Description​

public self Pdo::setIncrement( string $column, int|float $by )

Adds "column" = "column" + ? to an update, so the addition happens in the database and simultaneous requests do not lose counts. Model::increment() uses it.

Throws InvalidArgumentException when $column is not a plain name or $by is not an int or a float.

Arguments​

NameData typeDefaultRemarks
$columnstringNumeric column
$byint/floatAmount to add; negative to subtract

Example​

Controller
$db = new \Fegg\Database\Pdo();

// Update "cms_news" Set modified = ?, "view_count" = "view_count" + ? Where news_id = ?
$db->setIncrement('view_count', 1)->setWhere('news_id = ?', 5)->update('cms_news')->execute();