fabiang/doctrine-switch-connection

This package is abandoned and no longer maintained. No replacement package was suggested.

Switch your Doctrine connection depending on some others object value

v1.0.0 2021-02-11 11:02 UTC

This package is auto-updated.

Last update: 2022-08-11 14:37:33 UTC


README

Build Status

This Laminas and Zend Framework 3 module helps you to switch your Doctrine connection depending on some others object value.

composer require fabiang/doctrine-switch-connection

Add the module to your application.config.php:

<?php

return [
    'modules'                 => [
        /** 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

use Doctrine\DBAL\Driver\PDOMySql\Driver as MySQLDriver;
use Fabiang\Common\SwitchDatabase\Doctrine\ConnectionFactory;

return [
    'doctrine' => [
        // 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'
            ]
        ],
    ],
];