PHP SDK - Data#
Read#
// All rows
$db->from('profiles')->execute();
// Select columns (aliases supported)
$db->from('profiles')->select('id, full_name, created_at')->execute();
// Single row by primary key
$db->from('profiles')->get('9f8a7b6c-...');
// First matching row, or null
$db->from('profiles')->eq('phone', '+255712345678')->single();
// Row count only (no rows transferred)
$db->from('profiles')->head(); // data: ['count' => 42]
PHPFilters#
| Method | SQL |
|---|---|
eq($col, $v) | col = v |
neq($col, $v) | col != v |
gt($col, $v) / gte($col, $v) | col > v / col >= v |
lt($col, $v) / lte($col, $v) | col < v / col <= v |
like($col, '%x%') | LIKE |
ilike($col, '%x%') | ILIKE (case-insensitive) |
isNull($col) / isNotNull($col) | IS NULL / IS NOT NULL |
in($col, ['a','b']) | IN (...) |
Filters chain with AND.
Ordering and pagination#
$db->from('profiles')->order('created_at', ascending: false)->execute();
$db->from('profiles')->order('created_at')->limit(50)->offset(100)->execute();
PHPFor large tables use keyset pagination (O(1) per page vs O(n) for offset):
$page = $db->from('logs')->select('id')->order('id')->limit(100)->execute();
$lastId = end($page->data)['id'];
$next = $db->from('logs')->select('id')->order('id')->limit(100)->cursor($lastId)->execute();
PHPJoins#
Column-picker joins - no SQL needed:
use RafikiDB\JoinSpec;
$rows = $db->from('messages')
->select('id as message_id, message, users.full_name as sender')
->join(new JoinSpec(
table: 'users',
fromColumn: 'user_id',
toColumn: 'id',
type: 'LEFT JOIN', // INNER JOIN | LEFT JOIN | RIGHT JOIN | FULL JOIN
))
->limit(20)
->execute();
PHPJoinSpec(table:, fromColumn:, toColumn:, type:)builds the ON clauseon:raw ON clause (advanced; takes precedence over the column pair)- Joined columns are keyed as
{table}.{column}(or your alias) - Joins route through
POST /projects/{id}/query/run
Write#
// Insert one row
$db->from('profiles')->insert([
'full_name' => 'Asha Mwinyi',
'nationality' => 'Tanzania',
]);
// Insert many rows
$db->from('profiles')->insert([
['full_name' => 'Asha', 'nationality' => 'Tanzania'],
['full_name' => 'Juma', 'nationality' => 'Kenya'],
]);
// Update (requires an eq('id', ...) filter first)
$db->from('profiles')->eq('id', $id)->update(['nationality' => 'Kenya']);
// Delete (requires an eq('id', ...) filter first)
$db->from('profiles')->eq('id', $id)->delete();
PHPupdate() / delete() throw an InvalidArgumentException unless an
eq('id', ...) filter is set first.
Raw request#
For anything not covered by the builder, use the raw client:
$db->request('/projects/' . $db->client->projectId() . '/data/profiles', 'GET');
// or the shortcuts on the low-level client
$db->client->get($path, $query);
$db->client->post($path, $body);
$db->client->patch($path, $body);
$db->client->put($path, $body);
$db->client->delete($path);
PHPNext: Auth.