-
-
Notifications
You must be signed in to change notification settings - Fork 3
Type safe getters
This library includes a small set of helpers for common cases where a query returns one column and we already know the PHP type we want.
If a query returns one value, we can ask for that type directly:
fetchBool()fetchString()fetchInt()fetchFloat()fetchDateTime()
$username = $db->fetchString("user/getUsernameById", 105);
$score = $db->fetchFloat("score/calculateForUser", 8008);
$lastActivity = $db->fetchDateTime("user/getLatestActivity", 654);This removes the usual "fetch row, then extract one column" boilerplate.
If the query still returns one column, but many rows, there are matching array helpers:
fetchAllBool()fetchAllString()fetchAllInt()fetchAllFloat()fetchAllDateTime()
$userIds = $db->fetchAllInt("user/getAllIds");When we use fetch() or iterate over fetchAll(), each row also provides typed getters:
getString()getInt()getFloat()getBool()getDateTime()get()asArray()
$userRow = $db->fetch("user/getById", 105);
if($userRow) {
echo $userRow->getString("email");
}The getDateTime() helper accepts common database date strings as well as Unix timestamps.
Example: https://github.com/PhpGt/Database/blob/master/example/03-type-safe-getters.php
Next, move on to Database migrations to see how schema changes are tracked and applied safely.
PHP.GT/Database is a separately maintained component that powers database features in PHP.GT/WebEngine.