how to connect multiple databse

how to connect multiple database if user login he take data from first db then after login he tack data from second db and configuration setting done for second db .

so i am create the component for that and trying to call on controller but i am not getting what is happend

given below is my component code pls suggest me proper way

<?php

namespace common\components;

use Yii;

use yii\base\Component;

use yii\base\InvalidConfigException;

class AddConnection extends RActiveRecord {

public  &#036;dbadvert = null;





public  function Welcome()


{

echo "Hello…Welcome to MyComponent";

    if (self::&#036;dbadvert &#33;== null)


        return self::&#036;dbadvert;


    else


    {

// $User=User::model()->findByPk(Yii::app()->user->id);

// $db_name = $user->db_name;

         &#036;db_name = yiiadvanced;








         self::&#036;dbadvert = Yii::createComponent(array(


         'class' =&gt; 'CDbConnection',


        // other config properties...

// ‘connectionString’=>“mysql:host=localhost;dbname=”.$db_name, //dynamic database name here

         'connectionString'=&gt;&quot;mysql:host=localhost;dbname=&quot;.&#036;db_name, //dynamic database name here


          'enableProfiling' =&gt; true,


          'enableParamLogging' =&gt; true,


          'username'=&gt;'root',


          'password'=&gt; '', //password here


          'charset'=&gt;'utf8',


          'emulatePrepare' =&gt; true,


          'enableParamLogging'=&gt;true,


          'enableProfiling' =&gt; true,


         ));


        Yii::app()-&gt;setComponent('dbadvert', self::&#036;dbadvert);





        if (self::&#036;dbadvert instanceof CDbConnection)


        {   


            Yii::app()-&gt;db-&gt;setActive(false);


            Yii::app()-&gt;dbadvert-&gt;setActive(true);


            return self::&#036;dbadvert;


        }


        else{


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


        }





    }


}

}

Hi!

I suggest you to read the Yii 2 Guide here:

http://www.yiiframework.com/doc-2.0/index.html

Even when you know Yii 1.

Always read the whole guide first.

Then you will quickly discover:

There is NO "CDbConnection" class anymore.

Also - when you enter your question into google, like for example:

You find plenty examples & answers how to achive what you want.

And finally:

Please learn how to format your code when posting in the forum.




// use the code tags below this line without the // at beginning.

// [code] your well formattet php code [//code]


// Then your code looks like this: 

public function example(){

  $test "Just a Test to show code formatting in the yii forum!";

  return $test;

}



There is also a button to format your code.

Regards

getting error when i using in code createcomponent how to solve this

PHP Fatal Error – yii\base\ErrorException

Call to undefined method Yii::createComponent()

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

‘class’ => ‘CDbConnection’,

// other config properties…

// ‘connectionString’=>“mysql:host=localhost;dbname=”.$db_name, //dynamic database name here

‘connectionString’=>“mysql:host=localhost;dbname=”.$db_name, //dynamic database name here

‘enableProfiling’ => true,

‘enableParamLogging’ => true,

‘username’=>‘root’,

‘password’=> ‘’, //password here

‘charset’=>‘utf8’,

‘emulatePrepare’ => true,

‘enableParamLogging’=>true,

‘enableProfiling’ => true,

));

Of course you can ignore my previous answer…

Proceed mixing up yii 1 and yii 2 code and wonder why it is not working…

But don’t wonder if nobody will answer.

OR you could READ my previous answer, start reading the guide and RECOGNIZE that yii-2 is NOT yii-1!

Then you will realize that there is NO Yii::createComponent in Yii2!

Yii 1: http://www.yiiframework.com/doc/api/1.1/YiiBase#createComponent-detail

Yii 2: http://www.yiiframework.com/doc-2.0/yii-baseyii.html#createObject()-detail

Components in Yii 2: http://www.yiiframework.com/doc-2.0/guide-concept-components.html

Regards