https://github.com/fabiang/doctrine-switch-connection
Laminas & Zend Framework module to switch the DB connection depending on a session value
https://github.com/fabiang/doctrine-switch-connection
doctrine laminas laminas-mvc zend-framework
Last synced: 3 months ago
JSON representation
Laminas & Zend Framework module to switch the DB connection depending on a session value
- Host: GitHub
- URL: https://github.com/fabiang/doctrine-switch-connection
- Owner: fabiang
- License: other
- Archived: true
- Created: 2021-02-11T10:25:45.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-11T11:08:57.000Z (about 5 years ago)
- Last Synced: 2024-11-28T23:47:53.494Z (over 1 year ago)
- Topics: doctrine, laminas, laminas-mvc, zend-framework
- Language: PHP
- Homepage:
- Size: 83 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# fabiang/doctrine-switch-connection
[](https://travis-ci.com/fabiang/doctrine-switch-connection)
This Laminas and Zend Framework 3 module helps you to switch your Doctrine connection depending on some others object value.
```bash
composer require fabiang/doctrine-switch-connection
```
Add the module to your `application.config.php`:
```php
[
/** more modules */
'Fabiang\Common\SwitchDatabase',
],
];
```
Make sure you have `DoctrineORMModule` loaded before, as this module depends on it.
## Configuration
Configure your `doctrine.local.php` like the following:
```php
[
// configure your database connections
'connection' => [
// use orm_default in your app and that factory will then return one of these
'orm_conn1' => [
'driverClass' => MySQLDriver::class,
'params' => [
'host' => getenv('DB_CONN1_HOST'),
'port' => getenv('DB_CONN1_PORT'),
'user' => getenv('DB_CONN1_USER'),
'password' => getenv('DB_CONN1_PASSWORD'),
'dbname' => getenv('DB_CONN1_DBNAME'),
'charset' => 'utf8mb4',
]
],
'orm_conn2' => [
'driverClass' => MySQLDriver::class,
'params' => [
'host' => getenv('DB_CONN2_HOST'),
'port' => getenv('DB_CONN2_PORT'),
'user' => getenv('DB_CONN2_USER'),
'password' => getenv('DB_CONN2_PASSWORD'),
'dbname' => getenv('DB_CONN2_DBNAME'),
'charset' => 'utf8mb4',
]
],
]
],
'service_manager' => [
// configure your connection factories
'factories' => [
'doctrine.connection.orm_conn1' => ConnectionFactory::class,
'doctrine.connection.orm_conn2' => ConnectionFactory::class,
]
],
'switch-database' => [
// map value received from object to connection name
'connection_mapping' => [
'Fabiang' => 'orm_conn2',
'Test1' => 'orm_conn1',
'Test2' => 'orm_conn1',
],
'default_connection' => 'orm_conn1',
// configure the object, from whom to receive the value
// the value returned is used by `connection_mapping` to map
// the value to the connection name
'session_service' => [
// the service name to receive from container
'name' => 'autentication_storage_session',
'key' => [
'name' => 'companyBrand', // property or method name
'type' => 'property', // 'property' or 'method'
]
],
],
];
```