SurrealQL statements against the database.
If we wish to create a new record in the database, we can use the create
method. The first argument is the table name and the second argument is an associative array with the column names and values.
$person = $db->create("person:tobie", [ "name" => "Tobie", "lastname" => "Morgan Hitchcock", "age" => 30, "hobbies" => ["reading", "coding"] ]);
After when you created a record, you can now use the select
method to fetch the newly created person. The first argument is the newly created person’s ID or a string which is the table name.
$person = $db->select($person->id);
Or you can fetch it manually by using the RecordID or RecordStringId.
// using the StringRecordId $id = StringRecordId::create("person:tobie"); $person = $db->select($id); // using the RecordId $id = RecordId::create("person", "tobie"); $person = $db->select($id);
To update a record, you can use the update
method. The first argument is the RecordID or a StringRecordId, and the second argument is an associative array with the column names and values. Updating a record can be done if 3 ways:
The update
method will replace the entire record with the new values. So make sure you include all the columns in the associative array.
$person = $db->update($person->id, [ "name" => "Tobie", "lastname" => "Morgan Hitchcock", "age" => 31 ]);
You can find more information about updating a record using the update method here.
The merge
method will merge the new values with the existing record. If the column already exists, it will be replaced with the new value.
$person = $db->merge($person->id, [ "age" => 31 ]);
You can find more information about updating a record using the merge method here.
The patch
method will update a field in a single or multiple record(s) based on the path provided.
$person = $db->patch($person->id, [ "path" => "/hobbies/0", "op" => "replace", "value" => "writing" ]);
You can find more information about updating a record using the patch method here.
To delete a record, you can use the delete
method. The first argument is the RecordID or a StringRecordId.
$db->delete($person->id);
or we can use the RecordId or StringRecordId to delete the record.
$id = StringRecordId::create("person:tobie"); $db->delete($id); $id = RecordId::create("person", "tobie"); $db->delete($id);