You are only configure the console.php Configuration . But Yii Web application use the main.php
Check your main.php file
its located on Your App folder -> Protected -> config ->main.php
Change Db connection on this file
Question
I have yii application,, and i want to change the database connection.. first, my app is connect to 'trackstar' database, and later i want to change to 'taskmanagement' database..
So i just simply change the dbname in my code :
<?php
// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Console Application',
// application components
'components'=>array(
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=taskmanagement',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
'authManager'=>array(
'class'=>'CDbAuthManager',
'connectionID'=>'db',
'itemTable' => 'tbl_auth_item',
'itemChildTable' => 'tbl_auth_item_child',
'assignmentTable' => 'tbl_auth_assignment',
),
),
);
but when i run the app i got error :
CDbCommand failed to execute the SQL statement: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'trackstar.tbl_auth_assignment' doesn't exist. The SQL statement executed was: SELECT * FROM
tbl_auth_assignment
WHERE userid=:userid
the thing i dont understand is that why is still connect to trackstar
database eventhough i just change the dbname to taskmanagement
thx before :)
Solution 2
You are only configure the console.php Configuration . But Yii Web application use the main.php
Check your main.php file
its located on Your App folder -> Protected -> config ->main.php
Change Db connection on this file
OTHER TIPS
I guess one your model queries written as straight SQL query.
EX:
SELECT * FROM databaseName.tableName
So, Even you change the database name in the config file these kinds of queries may not work. Please check your code in this way.
You can use below code to update database dynamically
if ($city == 'ABC') {
$databse = 'db_abc';
} elseif ($city == 'BCD') {
$databse = 'db_bcd';
} else {
$databse = 'default_db';
}
$db = Yii::$app->getDb();
$db->dsn = "mysql:host=localhost;dbname=$databse";
Hope this help to override database connection string