Giter Site home page Giter Site logo

doctrine-switch-connection's Introduction

fabiang/doctrine-switch-connection

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

doctrine-switch-connection's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.