polaris / pdo
PDO database adapter (SQLite, MySQL, PostgreSQL) for Polaris for PHP
Requires
- php: >=8.3
- ext-pdo: *
- polaris/core: ^0.11
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The PDO database adapter for Polaris for PHP:
PostgreSQL, MySQL and SQLite through prepared statements, savepoints for nested transactions,
plus the DDL exporter and the schema inspector behind polaris schema:export and
schema:diff.
Install
composer require polaris/pdo
Use
use Polaris\Pdo\PdoAdapter; $pdo = new PDO('pgsql:host=localhost;dbname=app', $user, $password); $adapter = new PdoAdapter($pdo); // the dialect is detected from the driver $polaris = Polaris::create(new Config(database: $adapter, /* ... */));
Create the tables once from the schema (or run polaris schema:export --target=sql:postgres
from polaris/cli and apply the output with your migration tool):
use Polaris\Authorization\PermissionCatalog; use Polaris\Authorization\PermissionCatalogSeeder; use Polaris\Contract\Dialect; use Polaris\Pdo\SqlSchema; foreach (SqlSchema::createAll(Dialect::Postgres) as $statement) { $adapter->exec($statement); } (new PermissionCatalogSeeder(new PermissionCatalog()))->seed($adapter, new DateTimeImmutable());
SchemaInstaller::create($adapter, catalog: $polaris->graph()->permissionCatalog()) does both in one
call. On a database Polaris is already installed on, after a plugin was added, createMissing() creates
only the tables the database lacks, in foreign-key order, seeds again (the seeder is idempotent) and
answers the tables it created; a table that exists is left as it is, and polaris schema:diff reports what
it lacks:
use Polaris\Pdo\SchemaInstaller; $created = SchemaInstaller::createMissing($adapter, catalog: $polaris->graph()->permissionCatalog());
Dialects: Dialect::Postgres, Dialect::Mysql, Dialect::Sqlite (turn PRAGMA foreign_keys
on). The adapter passes the Polaris conformance suite on all three.
License
MIT. Polaris for PHP is created and maintained by 2am.tech.