karol-dabrowski/doctrine-dynamic-connection
最新稳定版本:v1.0.0
Composer 安装命令:
composer require karol-dabrowski/doctrine-dynamic-connection
包简介
Doctrine Dynamic Connection library allows you to modify database connection parameters at runtime
README 文档
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
wrapperClassparameter with the name ofDynamicConnectionWrapperclass as a value to the array of database connection parameters. For simplicity, use::classkeyword. - Create and instance of
DynamicEntityManagerand 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.
统计信息
- 总下载量: 1.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-27