mj-zana / database
PDO connections, query builder, schema builder, migrations and unit of work for Zana applications
Requires
- php: ^8.5
- ext-pdo: *
- doctrine/dbal: ^4.4
- doctrine/migrations: ^3.9
- mj-zana/console: ^2.0
- mj-zana/core: ^2.11
- mj-zana/ddd: ^2.0
- psr/log: ^3.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
Suggests
- ext-pdo_mysql: For MySQL and MariaDB connections
- ext-pdo_pgsql: For PostgreSQL connections
- ext-pdo_sqlite: For SQLite connections
Provides
None
Conflicts
None
Replaces
None
- dev-main
- 2.37.x-dev
- v2.37.0
- v2.36.0
- v2.35.0
- v2.34.0
- v2.33.0
- v2.32.0
- v2.31.0
- v2.30.0
- v2.29.0
- v2.28.0
- v2.27.1
- v2.27.0
- v2.26.0
- v2.25.0
- v2.24.0
- v2.23.0
- v2.22.0
- v2.21.0
- v2.20.0
- v2.19.0
- v2.18.0
- v2.17.0
- v2.16.0
- v2.15.0
- v2.14.0
- v2.13.0
- v2.12.0
- v2.11.0
- v2.10.0
- v2.9.0
- v2.8.0
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v2.0.0-rc.1
- v2.0.0-beta.3
- v2.0.0-beta.2
- v2.0.0-beta.1
- v2.0.0-alpha.3
This package is auto-updated.
Last update: 2026-11-24 16:53:47 UTC
README
PDO connections, a query builder, a schema builder, module-aware migrations and a transactional unit of work for SQLite, MySQL/MariaDB and PostgreSQL. Requires PHP 8.5 or later.
composer require mj-zana/database
Configuration
Register DatabaseServiceProvider (and ConsoleServiceProvider for the
commands), then configure config/database.php:
return [ 'default' => 'main', 'connections' => [ 'main' => ['driver' => 'sqlite', 'database' => 'var/bmi.sqlite'], 'reporting' => ['driver' => 'mysql', 'host' => 'db', 'database' => 'bmi', 'username' => 'bmi', 'password' => '...'], ], ];
Relative SQLite paths are resolved against the application's base path.
Queries
public function __construct(private ConnectionInterface $db) {} $this->db->table('measurements') ->select('patient_id', 'bmi') ->where('bmi', '>=', 25) ->whereIn('patient_id', $ids) ->orderBy('taken_on', 'desc') ->limit(20) ->get(); $this->db->table('measurements')->insert(['patient_id' => 7, 'bmi' => 22.5, 'taken_on' => '2026-10-24']); $this->db->table('measurements')->where('id', 3)->update(['bmi' => 22.6]); $this->db->transaction(fn (ConnectionInterface $db) => /* ... */); // nested calls use savepoints
Values are always bound. update() and delete() without a where throw
unless you pass force: true. Query errors carry the SQL, never the bound
values.
Schema and migrations
Since 2.37, migrations run on Doctrine Migrations
(ADR-035). New migrations are Doctrine classes, which make:migration writes. A schema migration changes
tables:
// database/migrations/Version20261124120000.php namespace Database\Migrations; final class Version20261124120000 extends AbstractMigration { public function isTransactional(): bool { return !$this->platform instanceof AbstractMySQLPlatform; // a schema change commits there anyway } public function up(Schema $schema): void { $table = $schema->createTable('clinics'); $table->addColumn('id', Types::BIGINT, ['autoincrement' => true, 'unsigned' => true]); $table->setPrimaryKey(['id']); } public function down(Schema $schema): void { $schema->dropTable('clinics'); } }
A data migration (make:migration fix_launch_dates --data) changes rows, in a transaction on every
database. It has no down(), so it cannot be rolled back:
final class Version20261124130000 extends AbstractMigration { public function up(Schema $schema): void { $found = (int) $this->connection->fetchOne('SELECT COUNT(*) FROM case_studies WHERE summary = ?', [$old]); $this->warnIf($found === 0, 'No case study has the old summary: nothing to change.'); $this->addSql('UPDATE case_studies SET summary = ? WHERE summary = ?', [$new, $old]); } }
Read with $this->connection, which runs at once. Write with addSql(), which is queued until up()
returns, so after every read. Match the old value, so a database already changed by hand is left alone.
Migration files written before 2.37 keep running unchanged, beside the Doctrine classes, and what ran is
still recorded in zana_migrations:
// database/migrations/2026_10_24_120000_create_measurements_table.php return new class () extends Migration { public function up(Schema $schema): void { $schema->create('measurements', function (Blueprint $t) { $t->id(); $t->bigInteger('patient_id'); $t->decimal('bmi', 4, 1); $t->date('taken_on'); $t->timestamps(); $t->foreign('patient_id')->on('patients')->onDelete('cascade'); $t->index(['patient_id', 'taken_on']); }); } public function down(Schema $schema): void { $schema->drop('measurements'); } };
php bin/zana make:migration create_measurements_table
php bin/zana make:migration fix_launch_dates --data
php bin/zana migrate
php bin/zana migrate:status
php bin/zana migrate:rollback --step=1
Migrations come from database/migrations and from each module's
Infrastructure/Persistence/Migrations, both kinds in the order of the date
in their name. migrate shows what migrations say (write(), warnIf()).
- Transactions. A batch runs in one transaction on SQLite and PostgreSQL. On MySQL and MariaDB a schema change commits the transaction, so a failure leaves the earlier migrations applied. Each data migration still runs in its own transaction there, even after a schema change in the same run. A migration that mixes data and schema cannot be atomic on MySQL, and its error says so.
- Rollback.
migrate:rollbackover a batch that holds a migration with nodown()is refused before anydown()runs, andmigrate:statusmarks such migrations as irreversible. - Doctrine's own commands (
migrations:*) are not registered: Zana's commands,checkandinspectcover the workflow. - Identity. A Doctrine migration is recorded by its class name with its namespace. Never move or rename one that has run: it would run again as a new migration.
- One run at a time. Two
migrateruns on one database at the same moment are not serialized: run migrations from one deployment step only.
Unit of work
UnitOfWorkInterface (from mj-zana/ddd) resolves, per scope, to a
TransactionalUnitOfWork on the default connection.
New in 2.12: on Doctrine DBAL
The connection runs on Doctrine DBAL
(ADR-027). The query and schema builders keep their API and their guard
rails; DBAL compiles them for the database, and DbalConnection::dbal()
gives its connection to integrations that must share it.
// Reserve 3 seats only while sold + 3 <= capacity: one statement, safe under concurrency. $won = $db->table('ticket_types')->where('id', $id)->whereColumn('sold', '<=', 'capacity', -3)->increment('sold', 3); $db->table('rates')->upsert(['currency' => 'CDF', 'rate' => 2850], uniqueBy: 'currency'); // insert, or update rate $row = $db->transaction(fn ($tx) => $tx->table('orders')->where('id', $id)->lockForUpdate()->first());
increment()anddecrement()change a column relative to itself, with other columns in the same statement; likeupdate(), they need a where clause.whereColumn($first, $operator, $second, $offset)compares two columns.upsert($rows, $uniqueBy, $update):ON CONFLICTon PostgreSQL and SQLite,ON DUPLICATE KEY UPDATEon MySQL, where any unique key of the table detects the conflict.lockForUpdate()addsFOR UPDATE; SQLite has no row locks.DatabaseException::isRetryable()is true for a deadlock or a lock wait timeout.- MySQL counts changed rows, not matched ones: an update that writes the values a row already has returns 0.
Connection (PDO), its grammars and ConnectionFactory::createPdo() are
deprecated and go in 3.0.
License
MIT — see LICENSE.