Skip to content

v1 (stable)

/

Concepts

Data manipulation

The methods below create, read, update and delete records. Each one takes a table name or a record ID as its first argument. To run SurrealQL statements directly, see executing queries.

MethodDescription
$db->select($thing)Selects all records in a table, or a specific record
$db->create($thing, $data)Creates a record in the database
$db->insert($thing, $data)Inserts one or multiple records in the database
$db->insertRelation($thing, $data)Inserts one or multiple relations in the database
$db->update($thing, $data)Replaces the data of all records in a table, or a specific record
$db->upsert($thing, $data)Creates a record if it does not exist, or updates it if it does
$db->merge($thing, $data)Merges data into all records in a table, or a specific record
$db->patch($thing, $data, $diff)Applies JSON Patch changes to all records in a table, or a specific record
$db->delete($thing)Deletes all records in a table, or a specific record

Selects all records in a table, or a specific record, from the database.

Method Syntax
$db->select($thing)

Arguments

Type

Description

thingstring

,

RecordId

or

StringRecordId

The table name or a RecordId to select.

// Select all records from a table
$people = $db->select('person');

// Select a specific record from a table
$person = $db->select(new RecordId('person', 'h5wxrf2ewk8xjxosxtyc'));
$person = $db->select(new StringRecordId('person:h5wxrf2ewk8xjxosxtyc'));

After a record is created, pass its ID to fetch it. A StringRecordId or a RecordId made with create() selects the same record.

$person = $db->select($person->id);

// using the StringRecordId
$id = StringRecordId::create("person:tobie");
$person = $db->select($id);

// using the RecordId
$id = RecordId::create("person", "tobie");
$person = $db->select($id);

This function will run the following query in the database.

SELECT * FROM $thing;

Creates a record in the database.

Method Syntax
$db->create($thing, $data)

Arguments

Type

Description

thingstring

,

RecordId

or

StringRecordId

The table name or a RecordId to create.

datamixed

The document / record data to create.

// Create a record with a random ID
[$person] = $db->create('person');

// Create a record with a specific ID
$person = $db->create(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
	"settings" => [
		"active" => true,
		"marketing" => true,
	],
]);

// The content you are creating the record with might differ from the return type
[$record] = $db->create(
    new RecordId('person', 'tobie'),
    ["name" => "Tobie"]
);

The second argument is an associative array with the field names and values.

$person = $db->create("person:tobie", [
	"name" => "Tobie",
	"lastname" => "Morgan Hitchcock",
	"age" => 30,
	"hobbies" => ["reading", "coding"]
]);

This function will run the following query in the database.

CREATE $thing CONTENT $data;

Inserts one or multiple records in the database.

Method Syntax
$db->insert($thing, $data)

Arguments

Type

Description

thingstring

The table name or RecordId to insert to.

dataassociative array

Either a single document/record or an array of documents/records to insert

// Insert a single record
[$person] = $db->insert('person', [
	"name" => 'Tobie',
	"settings" => [
		"active" => true,
		"marketing" => true,
	],
]);

$person = $db->insert(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
	"settings" => [
		"active" => true,
		"marketing" => true,
	],
]);

// Insert multiple records
$people = $db->insert('person', [
	[
		"name" => 'Tobie',
		"settings" => [
			"active" => true,
			"marketing" => true,
		],
	],
	[
		"name" => 'Jaime',
		"settings" => [
			"active" => true,
			"marketing" => true,
		],
	],
]);

// The content you are creating the record with might differ from the return type
$people = $db->insert('person', [
	[ "name" => 'Tobie' ],
	[ "name" => 'Jaime' ],
]);

This function will run the following query in the database.

INSERT INTO $thing $data;

Inserts one or multiple relations in the database.

Method Syntax
$db->insertRelation($thing, $data)

Arguments

Type

Description

thing

string or Table

Target table to insert the relation to.

data

array

An array of relations to insert.

// Insert a single relation
$relation = $db->insertRelation('person', [
	"id" => new RecordId('person', 'tobie'),
	"in" => new RecordId('company', 'surreal'),
	"out" => new RecordId('role', 'founder'),
]);

// Insert multiple relations
$relations = $db->insertRelation('person', [
	[
		"id" => new RecordId('person', 'tobie'),
		"in" => new RecordId('company', 'surreal'),
		"out" => new RecordId('role', 'founder'),
	],
	[
		"id" => new RecordId('person', 'jaime'),
		"in" => new RecordId('company', 'surreal'),
		"out" => new RecordId('role', 'cofounder'),
	],
]);

Updates all records in a table, or a specific record, in the database.

Method Syntax
$db->update($thing, $data)
Note

This function replaces the current document / record data with the specified data.

Arguments

Type

Description

thingstring

,

RecordId

or

StringRecordId

The table name or the specific RecordId to update.

datamixed

The document / record data to update.

// Update all records in a table
$people = $db->update('person');

// Update a record with a specific ID
$person = $db->update(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
	"settings" => [
		"active" => true,
		"marketing" => true,
	],
]);

// The content you are updating the record with might differ from the return type
$record = $db->update(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
]);

This call replaces the entire record with the new values, so it lists every field. See the UPDATE statement reference for more.

$person = $db->update($person->id, [
	"name" => "Tobie",
	"lastname" => "Morgan Hitchcock",
	"age" => 31
]);

This function will run the following query in the database.

UPDATE $thing CONTENT $data;

Creates or updates a record in a table.

Method Syntax
$db->upsert($thing, $data);

Arguments

Type

Description

$thing

RecordId. StringRecordId or string

The collection to upsert the record to.

$data

mixed

The record to upsert.

Example
$id = new RecordId('users', 'tobie');

// Upsert a record to the "users" collection.
$response = $db->upsert($id, [
	'firstname' => 'Tobie',
	'lastname' => 'Hitchcock',
]);

Modifies all records in a table, or a specific record, in the database.

Method Syntax
$db->merge($thing, $data)
Note

This function merges the current document / record data with the specified data.

Arguments

Type

Description

thingstring

,

RecordId

or

StringRecordId

The table name or the specific RecordId to merge.

datamixed

The document / record data to merge.

// Update all records in a table
$people = $db->merge('person', [
	"updated_at" => new Date(),
]);

// Update a record with a specific ID
$person = $db->merge(new RecordId('person', 'tobie'), [
	"updated_at" => new Date(),
	"settings" => [
		"active" => true,
	],
]);

// The content you are merging the record with might differ from the return type
$record = $db->merge(new RecordId('person', 'tobie'), [
	"name" => 'Tobie',
]);

The new values are merged with the existing record, and a field that already exists is replaced with the new value. See the RPC protocol reference for more.

$person = $db->merge($person->id, [
	"age" => 31
]);

This function will run the following query in the database.

UPDATE $thing MERGE $data;

Applies JSON Patch changes to all records, or a specific record, in the database.

Method Syntax
$db->patch($thing, $data, $diff)
Note

This function patches the current document / record data with the specified JSON Patch data.

Arguments

Type

Description

thingstring

,

RecordId

or

StringRecordId

The table name or the specific RecordId to patch.

dataassociative array

The JSON Patch data with which to patch the records.

diffboolean

Whether to return the diff of the patched record.

// Update all records in a table
$people = $db->patch('person', [
		[ "op" => 'replace', "path" => '/created_at',
	    "value" => new Date() ],
]);

// Update a record with a specific ID
$person = $db->patch(new RecordId('person', 'tobie'), [
		[ "op" => 'replace', "path" => '/settings/active',
	    "value" => false ],
		[ "op" => 'add', "path" => '/tags', "value" => ['developer',
	    'engineer'] ],
	[ "op" => 'remove', "path" => '/temp' ],
]);

This function will run the following query in the database.

UPDATE $thing PATCH $data;

Deletes all records in a table, or a specific record, from the database.

Method Syntax
$db->delete($thing)

Arguments

Type

Description

thingstring

,

RecordId

or

StringRecordId

The table name or a RecordId to delete.

// Delete all records from a table
$db->delete('person');

// Delete a specific record from a table
$db->delete(new RecordId('person', 'h5wxrf2ewk8xjxosxtyc'));

The first argument can also be a RecordId or a StringRecordId.

$db->delete($person->id);

$id = StringRecordId::create("person:tobie");
$db->delete($id);

$id = RecordId::create("person", "tobie");
$db->delete($id);

This function will run the following query in the database.

DELETE $thing;

Was this page helpful?

Last updated