karol-dabrowski / doctrine-dynamic-connection
Doctrine Dynamic Connection library allows you to modify database connection parameters at runtime
Installs: 1 547
Dependents: 0
Suggesters: 0
Security: 0
Stars: 11
Watchers: 2
Forks: 1
Open Issues: 2
Requires
- php: >=7.3
- doctrine/common: ^3.0
- doctrine/dbal: ^2.10.0
- doctrine/orm: ^2.8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.18
- phpstan/phpstan: ^0.12.81
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2025-04-28 19:37:21 UTC
README
Doctrine Dynamic Connection is a library that allows you to dynamically reinitialize a database connection with different parameters. The library creates wrappers for Connection
and EntityManager
classes and extend their functionality.
Requirements
- PHP 7.3 or greater
Installation
To install Doctrine Dynamic Connection via Composer execute the following command:
composer require karol-dabrowski/doctrine-dynamic-connection
Setup
To use the library features, you need to make two changes comparing to the basic Doctrine ORM configuration:
- Add
wrapperClass
parameter with the name ofDynamicConnectionWrapper
class as a value to the array of database connection parameters. For simplicity, use::class
keyword. - Create and instance of
DynamicEntityManager
and pass your default entity manager as a constructor argument.
<?php // bootstrap.php require_once 'vendor/autoload.php'; use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\EntityManager; use DynamicConnection\DynamicConnectionWrapper; use DynamicConnection\DynamicEntityManager; // the connection configuration $dbParams = array( 'driver' => 'pdo_mysql', 'user' => 'root', 'password' => 'pass', 'dbname' => 'db_name', 'wrapperClass' => DynamicConnectionWrapper::class ); $isDevMode = true; $proxyDir = null; $cache = null; $useSimpleAnnotationReader = false; $config = Setup::createAnnotationMetadataConfiguration( array(__DIR__."/src"), $isDevMode, $proxyDir, $cache, $useSimpleAnnotationReader ); // For XML mappings // $config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode); // For YAML mappings // $config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode); $entityManager = EntityManager::create($dbParams, $config); $dynamicEntityManager = new DynamicEntityManager($entityManager);
Usage
The most important method of DynamicEntityManager
is modifyConnection()
. It takes five parameters, but none of them is required.
public function modifyConnection( ?string $databaseName = null, ?string $username = null, ?string $password = null, ?string $host = null, ?string $port = null ): void;
Only parameters with non-null value will be modified. If you call the method with no arguments, your connection parameters will not be changed. Pass null
when you don't want to change a particular parameter.
<?php // Change database name $dynamicEntityManager->modifyConnection('new_db_name'); // Change database name and database user $dynamicEntityManager->modifyConnection('new_db_name', 'username', 'password'); // Change database user and leave database name unchanged $dynamicEntityManager->modifyConnection(null, 'username', 'password'); // Change only database host and port, leave database name and user unchanged $dynamicEntityManager->modifyConnection(null, null, null, '127.0.0.2', '3307'); // Change database name, host and port, leave database user unchanged $dynamicEntityManager->modifyConnection('new_db_name', null, null, '127.0.0.2', '3307'); // Change all parameters $dynamicEntityManager->modifyConnection('new_db_name', 'username', 'password', '127.0.0.2', '3307');
Author
Karol Dabrowski @kdabrowskidev
License
Released under the MIT license.