Skip to content

Latest commit

 

History

History
89 lines (67 loc) · 1.54 KB

File metadata and controls

89 lines (67 loc) · 1.54 KB

Numeric Helper Functions

Common mathematical operations with PDOdb helpers.

Increment/Decrement

Increment

use tommyknocker\pdodb\helpers\Db;

// Increment by 1 (default)
$db->find()->table('users')->where('id', 1)->update([
    'counter' => Db::inc()
]);

// Increment by specific amount
$db->find()->table('users')->where('id', 1)->update([
    'score' => Db::inc(5)
]);

Decrement

// Decrement by 1 (default)
$db->find()->table('users')->where('id', 1)->update([
    'stock' => Db::dec()
]);

// Decrement by specific amount
$db->find()->table('users')->where('id', 1)->update([
    'credits' => Db::dec(10)
]);

Absolute Value

$users = $db->find()
    ->from('transactions')
    ->select(['amount' => Db::abs('amount')])
    ->get();

Rounding

$products = $db->find()
    ->from('products')
    ->select(['price' => Db::round('price', 2)])
    ->get();

Modulo

$users = $db->find()
    ->from('users')
    ->select(['remainder' => Db::mod('score', 10)])
    ->get();

Common Patterns

Update Score

// Add 100 points
$db->find()->table('users')->where('id', $userId)->update([
    'score' => Db::inc(100)
]);

Decrease Stock

// Reduce stock by quantity
$db->find()
    ->table('products')
    ->where('id', $productId)
    ->update(['stock' => Db::dec($quantity)]);

Next Steps