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

Model::increment

Add to a numeric column

Description​

public int Model::increment( string $column, mixed $id, [ int|float $by = 1 ] )

Adds $by to $column of the row whose primary key is $id, in the UPDATE itself ("column" = "column" + ?), and returns the number of updated rows. Simultaneous requests do not lose counts, as they would when reading, adding and writing back.

Like every update, it also sets modified. Call unsetRegularUseQuery first to leave modified alone (for example for a view counter).

Throws InvalidArgumentException when $column is not a plain name or $by is not a number.

Arguments​

NameData typeDefaultRemarks
$columnstringNumeric column
$idmixedTarget primary ID
$byint/float1Amount to add; negative to subtract

Example​

Controller
// The table has a view_count INTEGER column
$model = new Model(false, array(
'table' => 'cms_news',
'id' => 'news_id',
'visible' => 'visible',
));

// Update "cms_news" Set "view_count" = "view_count" + 1 Where news_id = 24
$model->unsetRegularUseQuery()->increment('view_count', 24);

// Subtract 2 (modified is updated)
$model->increment('view_count', 24, -2);