Introduction
Windwalker Database is a DBAL component. It wraps PDO, MySQLi, PostgreSQL, SQL Server and SQLite drivers behind one API, and it hides most platform differences from your code. Use it when you want direct control over SQL, transactions and schema changes without an object mapper. It is also the foundation of the ORM component.
The package is built from four parts:
DatabaseFactorycreates aDatabaseAdapterfrom a driver name andDriverOptions.DatabaseAdapteris the entry point. It exposes adriverfor connections and statements, and aplatformfor SQL dialect, transactions and DDL.WriterManager, available as$db->writer, inserts, updates, upserts and deletes rows.TableManager,SchemaManagerandDatabaseManagercreate, inspect and change tables and databases.
Installation
composer require windwalker/database ^4.0Windwalker Database 4.x requires PHP 8.4.6 or later.
Use as Standalone Component
Create an adapter with DatabaseFactory. The driver name selects the driver and the platform, for example pdo_mysql or pdo_sqlite:
use Windwalker\Database\DatabaseFactory;
use Windwalker\Database\Driver\DriverOptions;
$db = new DatabaseFactory()->create(
'pdo_mysql',
new DriverOptions(
host: 'localhost',
dbname: 'app',
user: 'root',
password: 'secret',
charset: 'utf8mb4',
),
);
$title = $db->prepare('SELECT title FROM posts WHERE id = ?')
->execute([1])
->result();See Connections for all driver names and options.
Use in Windwalker
DatabasePackage binds DatabaseAdapter as a shared service. The adapter is created from the database configuration, and each connection is available through a tag named after the connection. Use the existing container or application; do not create a new Container.
Inject the adapter as a constructor dependency:
use Windwalker\Database\DatabaseAdapter;
class PostRepository
{
public function __construct(
protected DatabaseAdapter $db
) {
}
}Or retrieve it from the container or the application:
use Windwalker\Database\DatabaseAdapter;
$db = $container->get(DatabaseAdapter::class);
$db = $container->get(DatabaseAdapter::class, tag: 'local');
$db = $app->retrieve(DatabaseAdapter::class);The ORM is also available from the adapter as $db->orm.
Configuration
The package ships etc/database.config.php. Override it in the host app by placing a file with the same name in etc/packages/:
// etc/packages/database.config.php
use Windwalker\Core\Attributes\ConfigModule;
use Windwalker\Database\DatabasePackage;
use Windwalker\Database\Driver\DriverOptions;
return #[ConfigModule(
name: 'database',
enabled: true,
priority: 100,
belongsTo: DatabasePackage::class,
)]
static fn() => [
'default' => 'local',
'connections' => [
'local' => [
'driver' => env('DATABASE_DRIVER'),
'options' => fn() => new DriverOptions(
host: env('DATABASE_HOST') ?: 'localhost',
dbname: env('DATABASE_NAME'),
user: env('DATABASE_USER'),
password: env('DATABASE_PASSWORD'),
prefix: env('DATABASE_TABLE_PREFIX'),
),
],
],
];Each entry under connections uses the same keys as the standalone example. A pool key can be added for connection pooling in a Swoole server. See Connections.
Core Concepts
Windwalker Database keeps SQL explicit. You write SQL strings or build queries with the Query component, then run them through the adapter. The adapter does not hide the table structure: writers and schema managers work on plain table and column names.
Platform differences are handled by the platform object. Quoting, schema introspection, DDL syntax and transaction commands change per database, but the calling code stays the same.
Topic Pages
- Connections: driver names, options, read-only mode, reconnects and connection pools.
- Queries: executing SQL, fetching results, hydration and transactions.
- Hydrators: converting rows to objects, and writing a custom hydrator.
- Writer: inserting, updating, upserting and deleting rows.
- Tables: creating and changing tables with the
Schemabuilder. - Schema Diff: comparing a desired table definition with the database, and applying or previewing the changes.
- Databases and Schemas: creating and dropping databases, and inspecting schemas.
- Events: query and hydration events.