1. Go to this page and download the library: Download johnnyjoy/uda library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
johnnyjoy / uda example snippets
declare(strict_types=1);
namespace App\Repository;
use UDA\Link;
final class UserRepository
{
use Link;
protected static string $connection = 'app';
public function findName(int $id): ?string
{
$name = $this->value(
'SELECT name FROM users WHERE id = :id',
['id' => $id],
['users']
);
return is_string($name) ? $name : null;
}
}
use UDA\Database;
$db = Database::connectDefault();
$user = $db->row(
'SELECT id, name FROM users WHERE id = :id',
['id' => 42],
['users']
);
$names = $db->values(
'SELECT name FROM users WHERE active = :active ORDER BY name',
['active' => 1],
['users']
);
// Large result sets — callback per row, not one giant array
$db->each(
'SELECT id, payload FROM events WHERE processed = :p',
['p' => 0],
function (array $row): void {
// process one row
},
['events']
);
$affected = $db->exec(
'UPDATE users SET name = :name WHERE id = :id',
['name' => 'Ada', 'id' => 42],
['users']
);
$inserted = $db->returning(
'INSERT INTO users (name) VALUES (:name) RETURNING id',
['name' => 'Ada'],
['users']
);
$db->transaction(function (Database $db): void {
$db->exec(
'INSERT INTO users (id, name) VALUES (:id, :name)',
['id' => 1, 'name' => 'Ada'],
['users']
);
});
// Read terminator straight off WHERE (proxy end)
$name = $db->select()->from('users')->where('id', 42)->value();
// ORDER BY after WHERE — explicit end
$rows = $db->select('id', 'name')
->from('users')
->where('active', 1)
->end()
->orderBy('name')
->rows();
// UPDATE — end before exec
$db->update()
->table('users')
->set('name', 'Ada')
->where('id', 42)
->end()
->exec();