CodeIgniter Forums
want to use multiple database. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: want to use multiple database. (/showthread.php?tid=49470)



want to use multiple database. - El Forum - 02-20-2012

[eluser]Mistry007[/eluser]
hi every one. I want to use multiple database in my project. one db for the testing and another for real data. I have gone through the database config now i have set the two connection in database.php file

Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = '192.168.1.99';
$db['default']['username'] = 'jignesh';
$db['default']['password'] = 'pass';
$db['default']['database'] = 'xit_product';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['default']['save_queries'] = TRUE;




$db['default']['hostname'] = '192.168.1.99';
$db['default']['username'] = 'jignesh';
$db['default']['password'] = 'pass';
$db['default']['database'] = 'xit_product_test';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['default']['save_queries'] = TRUE;


Now i want to use it like if i run my test controller then it should take "xit_product_test" database. and if i run another controller then it will took the default database. now i have a little bit confustion for testing i have implemented unit testing
which check the model functions.
Do i have to change all model for multiple database.
Any help will be appriciated..
Thanks



want to use multiple database. - El Forum - 02-21-2012

[eluser]louisl[/eluser]
Check in the index.php for the ENVIRONMENT constant, I think that was added in 2.0.1
Then just switch your ./application/config/database.php depending on the environment eg.
Code:
switch (ENVIRONMENT) {

case 'development' :

  $db['default']['username'] = 'xxx';
  $db['default']['password'] = 'xxx';
  $db['default']['database'] = 'xxx';
  
break;

case 'testing' :
case 'production' :

  $db['default']['username'] = 'yyy';
  $db['default']['password'] = 'yyy';
  $db['default']['database'] = 'yyy';
  
break;

default:

  exit('The application environment is not set correctly.');
  
break;

}



want to use multiple database. - El Forum - 02-21-2012

[eluser]CroNiX[/eluser]
Actually, you create a subdirectory named after the environment and place the database.php config file (and other config files) in those. They will get loaded depending on the environment. No need for manual switching...

/application/config/testing/config.php
/application/config/testing/database.php

/application/config/production/config.php
/application/config/production/database.php

http://ellislab.com/codeigniter/user-guide/libraries/config.html#environments