How to use two database in one application

Hi Team,

Can you please help me…I have some doubts.Below mentioned the list

  1. Login process using one database after that using another database how to do this process.

  2. After login second database dynamically changing.

  3. It is possible login based database selection

A simple approach would be to setup the secondary database in config/web.php under components much like you setup your primary database having that setup.


<?php 


return [

// ...

    'components' => [

        // ...

        'db' => [

            'class' => 'yii\db\Connection',

            'dsn' => 'mysql:host=localhost;dbname=users_databaes',

            'username' => 'root',

            'password' => '',

            'charset' => 'utf8',

        ],

        'db2' => [

            'class' => 'yii\db\Connection',

            'dsn' => 'mysql:host=localhost;dbname=prdoucts_database',

            'username' => 'root',

            'password' => '',

            'charset' => 'utf8',

        ]

        // ...

    ]

// ...

];




how do we make use of these connections is simply matter of chaning getDb in your user model





class User extends ActiveRecord

{

    public static function getDb()

    {

        // use the "db2" application component

        return \Yii::$app->db2;  

    }

}



Your other models will user db by default, I think that should pretty straight forward to understand

thanks for your reply…but i am asking second db dynamic selection after second db choose over all application second db only active.how to do…