-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
This page takes you end-to-end in about five minutes. Every snippet runs unchanged against SQLite in-memory — no server, no fixtures, no setup.
InitORM\DBAL\Connection\Connection is the single entry point. Its
constructor accepts an associative credentials array that is merged on
top of sensible defaults.
use InitORM\DBAL\Connection\Connection;
$db = new Connection([
'driver' => 'sqlite',
'database' => ':memory:',
'charset' => '',
]);Nothing has happened on the network yet.
Connectionis lazy — the PDO instance is created the first time you callgetPDO(),query(), or any forwarded method.
$db->getPDO()->exec(
'CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
active INTEGER NOT NULL DEFAULT 1
)'
);
$db->query(
'INSERT INTO users (name, email) VALUES (:name, :email)',
['name' => 'Alice', 'email' => 'alice@example.com']
);
$id = (int) $db->lastInsertId(); // PDO method, forwardedNotes:
-
query()accepts named parameters as an associative array. The leading colon on each key is optional. - Values are bound with type-aware PDO constants. See Querying for the full mapping.
-
lastInsertId()is not defined onConnection— it lives on PDO and is forwarded via__call(). The same is true forbeginTransaction(),commit(),rollBack(),quote(), and friends.
$row = $db->query('SELECT id, name FROM users WHERE id = :id', ['id' => $id])
->asAssoc()
->row();
// ['id' => 1, 'name' => 'Alice']Three things happen on the read:
-
query()prepares the statement and binds:idwithPARAM_INT. - It returns a
DataMapperInterface. -
asAssoc()sets the fetch mode (this is sticky);row()returns the next row, ornullwhen there isn't one.
rows() returns every remaining row as an array (possibly empty):
$users = $db->query('SELECT id, name FROM users')->asAssoc()->rows();
// [['id' => 1, 'name' => 'Alice'], ...]final class User
{
public int $id = 0;
public string $name = '';
public string $email = '';
}
$users = $db->query('SELECT id, name, email FROM users')
->asClass(User::class)
->rows();
// $users is User[]asObject() and asLazy() are also available — see DataMapper.
$db->beginTransaction();
try {
$db->query('INSERT INTO orders (user_id, total) VALUES (:u, :t)', ['u' => 1, 't' => 99]);
$db->query('INSERT INTO order_items (order_id, sku) VALUES (:o, :s)',
['o' => $db->lastInsertId(), 's' => 'X-1']);
$db->commit();
} catch (\Throwable $e) {
if ($db->inTransaction()) {
$db->rollBack();
}
throw $e;
}There is no special transaction API — beginTransaction, inTransaction,
commit, and rollBack are all forwarded to PDO. See Transactions
for the gotchas around persistent connections.
Switch on query logging when you need to inspect activity:
$db->setQueryLogs(true);
$db->query('SELECT 1');
$db->query('SELECT 2');
foreach ($db->getQueryLogs() as $entry) {
echo $entry['query'], ' took ', $entry['timer'], "s\n";
}The buffer lives in process memory. For production, prefer a real logger — see Logging.
-
Connection — every credential, the lifecycle rules, and how
clone()works. - Querying — bind types, prepare options, error handling.
- DataMapper — fetch modes and result extraction.
InitORM DBAL · MIT · maintained by Muhammet ŞAFAK · part of the InitORM stack
Getting Started
Core
Cross-Cutting
Reference
Upgrading
Project