Update JSON values and structure using PDOdb.
Update a JSON field by setting a value at a specific path:
// Update city in meta JSON
$db->find()
->table('users')
->where('id', 1)
->update([
'meta' => $db->find()->jsonSet('meta', ['city'], 'London')
]);// Update nested value
// meta: { profile: { address: { city: "NYC" } } }
$db->find()
->table('users')
->where('id', 1)
->update([
'meta' => Db::jsonSet('meta', ['profile', 'address', 'city'], 'Boston')
]);Remove a path from JSON:
// Remove a field from meta
$db->find()
->table('users')
->where('id', 1)
->update([
'meta' => Db::jsonRemove('meta', ['old_field'])
]);// Remove from JSON array
$db->find()
->table('users')
->where('id', 1)
->update([
'tags' => Db::jsonRemove('tags', [1]) // Remove index 1
]);Note: In SQLite, removing an array element sets it to null to preserve array indices.
use tommyknocker\pdodb\helpers\Db;
// Add tag to existing tags array
$db->find()
->table('users')
->where('id', 1)
->update([
'tags' => Db::jsonArray(Db::raw('JSON_EXTRACT(tags, "$[*]")'), 'new_tag')
]);// Add new field to existing object
$db->find()
->table('users')
->where('id', 1)
->update([
'meta' => Db::raw('JSON_OBJECT(
JSON_EXTRACT(meta, "$.existing_field"),
"new_value",
"additional_field", "additional_value"
)')
]);$db->find()
->table('users')
->where('active', 1)
->update([
'meta->city' => 'Updated City',
'meta->updated_at' => Db::now()
]);Replace a JSON value at a path (only if path exists, does not create missing paths):
// Replace existing value
$db->find()
->table('users')
->where('id', 1)
->update([
'meta' => Db::jsonReplace('meta', '$.status', 'inactive')
]);
// Try to replace non-existent path (won't create it)
$db->find()
->table('users')
->where('id', 1)
->update([
'meta' => Db::jsonReplace('meta', '$.nonexistent', 'value')
]);
// Path won't be created if it doesn't existDb::jsonSet(): Creates path if missing, always sets the valueDb::jsonReplace(): Only replaces if path exists, does not create missing paths
// jsonSet creates path if missing
$db->find()
->table('users')
->where('id', 1)
->update([
'meta' => Db::jsonSet('meta', '$.new_field', 'value') // Creates path
]);
// jsonReplace only replaces if path exists
$db->find()
->table('users')
->where('id', 1)
->update([
'meta' => Db::jsonReplace('meta', '$.another_field', 'value') // Won't create path
]);$db->find()
->table('users')
->where('id', $userId)
->update([
'preferences' => Db::jsonSet('preferences', ['theme'], 'dark'),
'updated_at' => Db::now()
]);// Increment counter in JSON metadata
$db->find()
->table('users')
->where('id', 1)
->update([
'meta' => Db::raw('JSON_SET(meta, "$.login_count",
COALESCE(JSON_EXTRACT(meta, "$.login_count"), 0) + 1)')
]);UPDATE users SET meta = JSON_SET(meta, '$.city', 'London');UPDATE users SET meta = jsonb_set(meta, '{city}', '"London"');UPDATE users SET meta = json_set(meta, '$.city', 'London');PDOdb handles these differences automatically.
- JSON Modification - Update JSON values, set, remove, append
- JSON Querying - Query JSON data
- JSON Filtering - Filter by JSON
- JSON Aggregations - JSON functions