PHP code example of fabiang / doctrine-switch-connection

1. Go to this page and download the library: Download fabiang/doctrine-switch-connection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fabiang / doctrine-switch-connection example snippets




return [
    'modules'                 => [
        /** more modules */
        'Fabiang\Common\SwitchDatabase',
    ],
];



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'
            ]
        ],
    ],
];