Sunday, August 2, 2015

Lumen setup - different database connection for read and write

In this tutorial, i will teach you how to setup lumen framework to connect into different database when the query action is either read or write.

Imagine we have 2 databases, both has database named testing.
First database let's called it db master, second database is called db slave. db slave data is replicate from db master. So both database's data are same.

We want the system connect to db master when write statements such as CREATE, UPDATE, DELETE, and connect to db slave when read statement such as SELECT.


1. add the following code in .env file (create one if not exists, put it same level as app folder):
#default connection name if not specify
DB_CONNECTION=normal
#database host for read statement
DB_READ_HOST=10.1.1.1
#database host for write statement
DB_WRITE_HOST=10.1.1.2
#database driver
DB_DRIVER=mysql

DB_DATABASE_NAME=testing
DB_USERNAME=root
DB_PASSWORD=password

DB_CHARSET=utf8
DB_COLLATION=utf8_unicode_ci
DB_PREFIX=null

2. create a new folder called config, same level with folder app.

3. create a new file called database.php, save it into config folder we have just created.

4. in database.php, add the following:
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_CLASS,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'normal'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [
        
        
    'normal' => [
        'read'      => [
            'host'  => env('DB_READ_HOST', 'localhost'),
        ],
        'write'     => [
            'host'  => env('DB_WRITE_HOST', 'localhost'),
        ],
        'driver'    => env('DB_DRIVER', 'mysql'),
        'database'  => env('DB_DATABASE_NAME', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => env('DB_CHARSET', 'utf8'),
        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
        'prefix'    => env('DB_PREFIX', ''),
    ],


    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',
];

Summary:
We have successfully created a connection called "normal". When this connection is using, read and write statements are actually connected to 2 different databases. You can create more connections in database.php and name it differently. eg: create a connection called "high" to set read and write statements are connected to a same database.



Note:
In order for your configuration values to be loaded, you will need to uncomment the Dotenv::load() method call in your bootstrap/app.php file.