Multiple Database

Hi everyone!!

I have many database in my system. When user logged in, it is checking user credential from central database and now i want to implement that based on the login id of user database should be selected.

Waiting for your reply.

You can add more db component in config/main.php and call it like this link :

Multi database connection in Yii

Thanks Chung Tran…

This link can be useful for me…

Again I want to ask that there are total 15-16 Database in my system. So is there any way that based on the login id particular database should be selected??

Once again thank you very much!!

Did you mean, user authen informations are save in multi Database? When an user log in your system, base on user’s id, you have to determine what DB is selected to check the authen info ?

I just wonder how’s your system realize where the user authen info is storing ? If it can determine that, I think in the entry I mentioned before, you can choose the first access way.

yes u can

create a class file under protected\components named MyActiveRecord.php

using the following code .




<?php

class MyActiveRecord extends CActiveRecord {

    

    private static $dbadvert = null;

 

    protected static function getAdvertDbConnection()

    {

		

        if (self::$dbadvert !== null)

            return self::$dbadvert;

        else

        {

			$agency=Agency::model()->findByAttributes(array('id'=>Yii::app()->user->id));

            

			self::$dbadvert = Yii::createComponent(array(

             'class' => 'CDbConnection',

            // other config properties...

             'connectionString'=>"mysql:host=localhost;dbname=".$agency->db_name,

				'enableProfiling' => true,

				'enableParamLogging' => true,

			    'username'=>'root',

				'password'=> '',

                //'password'=> '',

                'charset'=>'utf8',

                'emulatePrepare' => true,

                'enableParamLogging'=>true,

                'enableProfiling' => true,

             ));

			Yii::app()->setComponent('dbadvert', self::$dbadvert);

			

            if (self::$dbadvert instanceof CDbConnection)

            {

				Yii::app()->db->setActive(false);

                Yii::app()->dbadvert->setActive(true);

                return self::$dbadvert;

            }

            else{

                throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));

            }

			

		}

    }

}



change this line according to your use


$agency=Agency::model()->findByAttributes(array('id'=>Yii::app()->user->id));




extend all models from MyActiveRecord and call function getDbConnection()




class Claims extends MyActiveRecord {

    

    public function getDbConnection()

    {

        return self::getAdvertDbConnection();

    }



http://www.yiiframework.com/wiki/544/multiple-database-select-database-based-on-login-user-id/

When user makes a login at that time based on the user_id the DB will be selected and then onwards all the DB opeartions are handled from that DB only…In my system there are 15 users, so all users have different DB. and DB name of each user is their login id. Login credential are stored in one centralized DB and after successfull login DB will be changed and will be selected on login id.

Thanks…Now I can try this one also…

It seems good.

Also I am thinking of storing DB name in one session variable and just use that session in /config/main.php file. So while giving DB arguments( DB name), I can pass that session value.

Is it possible??

if the login id is the db name then why another session?

so Yii::app()->user->id is the db name.

you can also set another user session like




Yii::app()->user->setState('db_name', $user->db_name); // Setting Up the Agency Id of the Logged in User



in the UserIdentity ,at the time of login

then u will get that like


  Yii::app()->user->db_name;  

Thanks everyone!!!!

got it? :)

Call to undefined method Yii::create Component() i am gating fetal error after implementing this code .pls help me to solve this error.

Check syntax


Yii::createComponent(array(

             'class' => 'CDbConnection',

            // other config properties...

             'connectionString'=>"mysql:host=localhost;dbname=".$agency->db_name,

                                'enableProfiling' => true,

                                'enableParamLogging' => true,

                            'username'=>'root',

                                'password'=> '',

                //'password'=> '',

                'charset'=>'utf8',

                'emulatePrepare' => true,

                'enableParamLogging'=>true,

                'enableProfiling' => true,

             ));