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?
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
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();
}
}