Kotchasan Framework Documentation

Kotchasan Framework Documentation

Kotchasan\QueryBuilder\UpdateBuilder

EN 05 Feb 2026 06:23

Kotchasan\QueryBuilder\UpdateBuilder

UpdateBuilder creates UPDATE queries.

Usage

use Kotchasan\Database;

$db = Database::create();

// Basic UPDATE
$affected = $db->update('users')
    ->set([
        'status' => 'inactive',
        'updated_at' => date('Y-m-d H:i:s')
    ])
    ->where(['id', 1])
    ->execute();

echo "Updated {$affected} row(s)";

Main Methods

update()

$db->update('tablename')

set()

->set([
    'column1' => 'new_value1',
    'column2' => 'new_value2'
])

where()

->where(['column', 'value'])

Examples

Multiple Conditions

$db->update('products')
    ->set(['price' => 99.99])
    ->where(['category', 'electronics'])
    ->where(['stock', '>', 0])
    ->execute();

Increment/Decrement

// Decrease stock
$db->update('products')
    ->set(['stock' => \Kotchasan\Database\Sql::raw('stock - 1')])
    ->where(['id', $productId])
    ->execute();

Generating SQL (Debugging)

// Build SQL without executing
$conn = new \Kotchasan\Connection\Connection(new \Kotchasan\Connection\MySQLDriver());
$qb = new \Kotchasan\QueryBuilder\UpdateBuilder($conn);
$sql = $qb->update('users')
    ->set(['status' => 'inactive'])
    ->where(['id', 1])
    ->toSql();

// Expected: UPDATE `users` SET `status` = :status WHERE (`id` = :qb_p0)

Warnings

⚠️ Important: Always include where() to avoid updating all rows.

  • Avoid passing user input into raw SQL
  • Use parameter binding instead of string concatenation
  • Use Sql::raw() only for trusted expressions