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.
| Method | Description |
|---|---|
$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 |
->select()
Selects all records in a table, or a specific record, from the database.
$db->select($thing)Arguments
Arguments | Type | Description |
|---|---|---|
thing | string, RecordIdor StringRecordId | The table name or a |
Example usage
// 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);Translated query
This function will run the following query in the database.
SELECT * FROM $thing; ->create()
Creates a record in the database.
$db->create($thing, $data)Arguments
Arguments | Type | Description |
|---|---|---|
thing | string, RecordIdor StringRecordId | The table name or a |
data | mixed | The document / record data to create. |
Example usage
// 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"]
]);Translated query
This function will run the following query in the database.
CREATE $thing CONTENT $data; ->insert()
Inserts one or multiple records in the database.
$db->insert($thing, $data)Arguments
Arguments | Type | Description |
|---|---|---|
thing | string | The table name or |
data | associative array | Either a single document/record or an array of documents/records to insert |
Example usage
// 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' ],
]);Translated query
This function will run the following query in the database.
INSERT INTO $thing $data; ->insertRelation()
Inserts one or multiple relations in the database.
$db->insertRelation($thing, $data)Arguments
Arguments | Type | Description |
|---|---|---|
thing |
| Target table to insert the relation to. |
data |
| An array of relations to insert. |
Example usage
// 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'),
],
]); ->update()
Updates all records in a table, or a specific record, in the database.
$db->update($thing, $data)This function replaces the current document / record data with the specified data.
Arguments
Arguments | Type | Description |
|---|---|---|
thing | string, RecordIdor StringRecordId | The table name or the specific |
data | mixed | The document / record data to update. |
Example usage
// 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
]);Translated query
This function will run the following query in the database.
UPDATE $thing CONTENT $data; ->upsert()
Creates or updates a record in a table.
$db->upsert($thing, $data);Arguments
Arguments | Type | Description |
|---|---|---|
$thing |
| The collection to upsert the record to. |
$data |
| The record to upsert. |
Example usage
$id = new RecordId('users', 'tobie');
// Upsert a record to the "users" collection.
$response = $db->upsert($id, [
'firstname' => 'Tobie',
'lastname' => 'Hitchcock',
]); ->merge()
Modifies all records in a table, or a specific record, in the database.
$db->merge($thing, $data)This function merges the current document / record data with the specified data.
Arguments
Arguments | Type | Description |
|---|---|---|
thing | string, RecordIdor StringRecordId | The table name or the specific |
data | mixed | The document / record data to merge. |
Example usage
// 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
]);Translated query
This function will run the following query in the database.
UPDATE $thing MERGE $data; ->patch()
Applies JSON Patch changes to all records, or a specific record, in the database.
$db->patch($thing, $data, $diff)This function patches the current document / record data with the specified JSON Patch data.
Arguments
Arguments | Type | Description |
|---|---|---|
thing | string, RecordIdor StringRecordId | The table name or the specific |
data | associative array | The JSON Patch data with which to patch the records. |
diff | boolean | Whether to return the diff of the patched record. |
Example usage
// 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' ],
]);Translated query
This function will run the following query in the database.
UPDATE $thing PATCH $data; ->delete()
Deletes all records in a table, or a specific record, from the database.
$db->delete($thing)Arguments
Arguments | Type | Description |
|---|---|---|
thing | string, RecordIdor StringRecordId | The table name or a |
Example usage
// 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);Translated query
This function will run the following query in the database.
DELETE $thing;