karol-dabrowski/doctrine-dynamic-connection
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.
karol-dabrowski/doctrine-dynamic-connection 适用场景与选型建议
karol-dabrowski/doctrine-dynamic-connection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.1k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2021 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 karol-dabrowski/doctrine-dynamic-connection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 karol-dabrowski/doctrine-dynamic-connection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-27