sylvainduval / dynamic-db-bundle
Build and edit your database schema with PHP.
Requires
- php: >=8.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.85
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12
Suggests
- psr/container: Required if you use standalone bridge for dependency injection
- symfony/dependency-injection: Required if you use Symfony dependency injection
This package is auto-updated.
Last update: 2026-03-14 16:26:30 UTC
README
Object-Oriented Database Schema Management for PHP
Description
DynamicDB is a lightweight and expressive PHP bundle that enables you to manage database schemas dynamically through an object-oriented API.
Unlike traditional ORMs or migration systems that rely on static entity definitions and diffs, DynamicDB gives you full control over your schema operations — explicitly and programmatically.
This makes it ideal for applications where the database structure must adapt to dynamic or user-defined data models, such as:
- Product Information Management (PIM) systems
- Digital Asset Management (DAM) tools
- Reporting or analytics platforms
- Data mining or ETL tools
- Any schema-on-the-fly use case
With DynamicDB, you can:
- Create and drop databases or tables programmatically
- Define fields using PHP objects and set their attributes
- Add, modify or delete fields explicitly
- Run schema operations in install scripts, migrations, or runtime processes
Key Principles & Advantages
Clear Responsibilities
- You decide what should happen: create, modify, or delete.
- No hidden diffs or magic. DynamicDB executes exactly what you tell it.
Fluent and Readable API
- Object-oriented syntax makes your schema definition self-documented.
- Easy to version, organize, and replay (e.g. in migrations or CLI scripts).
Method-Based Flexibility
Core operations are exposed via stable and testable methods:
- createDatabase()
- deleteDatabase()
- createTable()
- createFields()
- changeField()
- deleteField()
- ...
Each schema component is represented as an object (Database, Table, Field), making it easy to encapsulate logic and manipulate structure dynamically. Database-specific settings for MySQL and PostgreSQL can be provided via dedicated options domain objects.
Lazy loading
No connection is opened, and no SQL is executed, until the first operation is explicitly called. This allows for deferred execution and better control over when and how the database is accessed.
Getting Started
Dependencies
- PHP >= 8.4
- PDO or mysqli driver
- MySQL >= 5.7, MariaDB >= 10.7 or PostgreSQL
Also suggested requirements for dependency injection :
- psr/container: Required if you use standalone bridge
- symfony/dependency-injection: Required if you use Symfony
Development Setup with Docker
For development, this project includes a complete Docker environment with PHP 8.4, MySQL, MariaDB, and PostgreSQL.
Prerequisites
- Docker and Docker Compose v2 installed on your system
Quick Start
-
Clone the repository and navigate to the project directory
-
Start the Docker environment:
make up
- Install dependencies:
make install
- Run tests to verify everything works:
make test
Available Make Commands
make help- Show all available commandsmake up- Start all Docker servicesmake down- Stop all Docker servicesmake shell- Open a shell in the PHP containermake install- Install Composer dependenciesmake test- Run all PHPUnit testsmake test-unit- Run only unit testsmake test-integration- Run only integration testsmake phpstan- Run PHPStan static analysismake cs-fix- Fix code style with PHP-CS-Fixermake cs-check- Check code style without fixingmake clean- Clean up containers, volumes, and caches
Manual Docker Commands
If you prefer not to use the Makefile:
# Start services docker compose up -d # Run tests docker compose exec php vendor/bin/phpunit # Run PHPStan docker compose exec php vendor/bin/phpstan analyse # Fix code style docker compose exec php vendor/bin/php-cs-fixer fix
Installing
- Reference it in your composer.json:
composer require sylvainduval/dynamic-db-bundle
- With Symfony
This bundle does not have a Symfony Flex recipe yet. You must add this line in config/bundles.php:
<?php return [ ... SylvainDuval\DynamicDbBundle\Bridge\Symfony\DynamicDbSymfonyBundle::class => ['all' => true], ];
And set up your database connection in config/packages/dynamic_db.yaml:
dynamic_db: host: 'db' port: 3306 user: '***' password: '***' database: null charset: 'utf8mb4'
- Without Symfony
Register the service in your own PSR container :
<?php require __DIR__ . '/../vendor/autoload.php'; $container = new App\Container(); $bundle = new \SylvainDuval\DynamicDbBundle\Bridge\Standalone\DynamicDbRegistrar(); $bundle->register([ 'host' => 'db', 'port' => 3306, 'user' => '***', 'password' => '***', 'database' => null, 'charset' => 'utf8mb4' ], $container);
Todo
Global:
- Improve exceptions and add query context in it
MySQL / MariaDB:
- Foreign keys contraints
- autoincrement UNIQUE, not only PRIMARY KEY
- Others database and table options
- ENUM field type
- DATE and DATETIME field: support option with timezone
- Add field: after / before
- ...
PostgreSQL:
- Schema management
- Foreign keys contraints
- Others database and table options
- ENUM field type
- JSON field: supports option binary version (JSONB)
- GEOMETRY and POINT fields: supports option for default value and SRID identifier
- DATE and DATETIME field: support option with timezone (TIMESTAMPTZ)
- Add field: after / before
- Change field: supports null / not null changes
- ...
Authors
Contributors names and contact info
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Acknowledgments
This project was inspired by several tools and concepts around schema versioning and database management:
- Liquibase – A powerful database schema change management tool that inspired the idea of declarative and trackable schema changes.
- Doctrine Migrations – For its structured approach to PHP-based migrations.
- Laravel Migrations – For its clean, fluent API to define schema changes in code.
- Flyway – For its emphasis on versioned migrations and repeatability.
Special thanks to the open-source community for the many ideas and design patterns that influenced this bundle.