• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
want to use multiple database.

#1
[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

#2
[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;

}

#3
[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-gui...vironments


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.