Download the PHP package feroz/dynamic-db-bundle without Composer
On this page you can find all versions of the php package feroz/dynamic-db-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download feroz/dynamic-db-bundle
More information about feroz/dynamic-db-bundle
Files in feroz/dynamic-db-bundle
Package dynamic-db-bundle
Short Description Symfony bundle to manage dynamic database connections using Doctrine ORM.
License MIT
Informations about the package dynamic-db-bundle
DynamicDbBundle
DynamicDbBundle is a Symfony bundle that allows you to store database connection configurations in a database table and seamlessly fetch and instantiate connections and entity managers at runtime.
With this bundle, you can natively use $doctrine->getConnection('dynamic_name') or $doctrine->getManager('dynamic_name') just like any conventionally configured connection in your doctrine.yaml.
Features
- Seamless Doctrine Integration: Fetches database configuration dynamically via Doctrine's
ManagerRegistry. - Inherited ORM Configuration: Dynamically boots new entity managers by automatically inheriting the configuration (metadata mapping drivers, naming strategies, custom functions, filters, etc.) of the default
EntityManager. No need to redefine entity paths or configs. - Dynamic Connection Discovery: Extends standard
ManagerRegistrylookup methods (getConnections(),getConnectionNames(),getManagers(), andgetManagerNames()) to include dynamically created connections/managers alongside static ones. - Universal Database Support: Supports all database drivers supported by Doctrine DBAL (MySQL, PostgreSQL, SQLite, Oracle, SQL Server, etc.) via structured parameter mapping or DSN URLs.
- Auto Cache Rebuilding: Automatically handles clearing the Symfony cache when your dynamic database connection entities are created, updated, or removed, ensuring any new connections are immediately discoverable.
- Runtime Secret Injection: Allows the consumer application to supply a decryption secret via Symfony's DI container, which is automatically forwarded to the connection entity via
setSecret()before the connection config is built.
Installation
Add the bundle to your project via Composer (if published):
Ensure the bundle is registered in your config/bundles.php:
Usage Guide
1. Create a Configuration Entity
Create a standard Doctrine Entity in your application that stores the database connection configurations. Crucially, this entity must implement DynamicDbConnectionInterface and provide implementations for all interface methods, including setSecret().
The setSecret() method is called automatically by DynamicDbProvider before building the connection config, allowing your entity to make the secret available to downstream logic (e.g., for custom password decryption).
2. Fetch the Dynamic Connection
You can fetch the connection or the manager directly through Symfony's core Doctrine integration! The bundle decorates the Doctrine registry to seamlessly integrate.
3. Password Encryption (Consumer Responsibility)
The bundle does not provide a built-in encryption utility. Password encryption and decryption are entirely the consumer's responsibility.
The recommended pattern is:
- Encrypt the password before persisting the entity (using any encryption library of your choice).
- Decrypt the password inside
getDatabasePassword()of your entity, using the secret injected viasetSecret().
4. Passing a Secret via Dependency Injection (for Encrypted Passwords)
DynamicDbProvider accepts $secret as an optional constructor parameter. The recommended approach is to bind it in your application's services.yaml using a Symfony parameter (e.g. from an environment variable):
With this configuration, DynamicDbProvider will automatically call $entity->setSecret($secret) on the fetched connection entity before building the connection config, giving the entity access to the secret for custom decryption logic.
If no secret is needed, simply omit the binding — the $secret parameter defaults to null and setSecret() will not be called.
How it Works
- When you call
$doctrine->getConnection('X')or$doctrine->getManager('X'), the wrappedDynamicRegistryDecoratorintercepts the request. - If Doctrine natively doesn't know about connection
X,DynamicDbProviderkicks in. - It finds the class implementing
DynamicDbConnectionInterfacedynamically and uses the default EntityManager to fetch the entity matchingconnectionName = 'X'. - If a
$secretwas configured (via DI), it calls$entity->setSecret($secret)on the fetched entity. - The
DynamicEntityManagerFactoryboots up the new ORM connection using the database connection parameters (or parses a DSN URL using Doctrine'sDsnParser). - The new dynamic
EntityManageris instantiated by inheriting the exact configuration (metadata mappings, naming strategies, proxies, etc.) of the defaultEntityManager. - Standard methods like
$doctrine->getConnections()or$doctrine->getManagers()are decorated to dynamically include the newly instantiated connections/managers alongside the statically defined ones. - The connection is cached locally for the remainder of the request.
All versions of dynamic-db-bundle with dependencies
doctrine/orm Version ^2.2||^3.3
symfony/framework-bundle Version ^5.4 || ^6.0 || ^7.0