How to change db name on the fly?

Hello everyone, in the project that I’m following at the moment I’ve a main page where I need to choose between a list of db name.

Every DB has the same table structure, the only difference is the database name.

How can I change the db name of my DBConnection on the fly and refresh the connection?

Is there a correct way?

Thank you very much.

Try something like :




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

Yii::app()->db->connectionString = 'mysql:host=localhost;dbname=mydb';

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



I will give a try as soon as possible :)

Do not forget to tell us how everything went as soon as possible.

I will play with this tomorrow :) So I will update everything tomorrow :)

It works but everything you have to set the database or when you change the page ( or refresh ) it will set the db information to default ( taking everything from db config in config/main.php ).

Is there a way to use mysql_select_db() in the connection?

Or make permanent the changes?

This obviously can’t be permanent as your config/main.php is loaded at every page request. Basicaly the only thing you can do is to store the informations in a file and include the file in your config/main.php

Hello everyone,

In which file should the following codes be?

After few hours, I was able to achieve my goal: of having a technique that will dynamically change the value of the database name, depending on the account that possesses the active session.

Yes, you heard me right, different database per account. No worries, these are corporate accounts, and scaling is for the purpose of db replication to a cloud-based service. Each corporate office is set up to have an offline db server that replicates asynchronously to a central db server.

With this issue, I came up with an extension that I called DynamicConnectionString, whose class is as follows:




/**

 * Description of ConnectionString

 * modify connection string according to database name

 *

 * @author dan

 */

class DynamicConnectionString {

        function init()

        {

                if (isset(Yii::app()->user->accountIdx) && !empty(Yii::app()->user->accountIdx))

                {

                        $ecompany = strtolower(Account::model()->findByPk(Yii::app()->user->accountIdx)->account_code);


                        try

                        {

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

                                Yii::app()->db->connectionString = 'mysql:host=127.0.0.1;dbname=medrec_' . $ecompany;

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

                        }

                        catch(Exception $e)

                        {

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

                                Yii::app()->db->connectionString = 'mysql:host=127.0.0.1;dbname=medrec';

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

                        }

                }

                else

                {

                        if (isset($_GET['c']) && !empty($_GET['c']))

                        {

                                $eCompany = strtolower($_GET['c']);

                        }


                        if (isset($eCompany) && !empty($eCompany))

                        {

                                try

                                {

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

                                        Yii::app()->db->connectionString = 'mysql:host=127.0.0.1;dbname=medrec_' . $ecompany;

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

                                }

                                catch(Exception $e)

                                {

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

                                        Yii::app()->db->connectionString = 'mysql:host=127.0.0.1;dbname=medrec';

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

                                }

                        }

                        else

                        {

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

                                Yii::app()->db->connectionString = 'mysql:host=127.0.0.1;dbname=medrec';

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

                        }

                }

        }

}



And since this should affect all controllers, I initialized the extension in the Controller class:




/**

 * Controller is the customized base controller class.

 * All controller classes for this application should extend from this base class.

 */

class Controller extends CController

{

	/**

	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',

	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.

	 */

	public $layout='//layouts/column1';

	/**

	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.

	 */

	public $menu=array();


	/**

	 * @var array the breadcrumbs of the current page. The value of this property will

	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}

	 * for more details on how to specify this property.

	 */

	public $breadcrumbs=array();


        function init()

        {

                /**

                 * @author dcj

                 * @description modify connection string according to database name

                 */

                Yii::app()->dynamicConnString->init();

        }

}



In config/main.php I added the following:




                'dynamicConnString'=>array(

                        'class'=>'application.extensions.dynamicconnectionstring.DynamicConnectionString'

                )



I hope his helps those who’ll require such solution.

Cheers!

i got this error message.

---->Property "CWebApplication.DynamicConnectionString" is not defined.<-----

how to fix the error? thanks

you have to create a folder called "DynamicConnectionString" and inside the folder, you put the php file "DynamicConnectionString.php"

by the way u could avoid putting manually the code in all controllers by calling the function inside /components/controller.php




class Controller extends CController

{


	function init()

        {

                Yii::app()->dynamicConnString->init();

        }



i hope still helps you