switching between multiple databases, what's wrong? |
[eluser]Unknown[/eluser]
Hello Peoples, I'm fairly new to CI having only a week under my belt but caught on pretty quickly, however the issue below has left my vexed. I have two databases and I have two models, A and B, that are called by one controller. I have a function/method in my controller that calls the two models. model A uses db A and Model B uses DB B. However when I call the second model from my controller it still uses db A and complains that the table doesn't exist (rightfully b/c its referencing the db A). ugh. Where should both databases be loaded ? on the controller or in the models when they are called? I have them loading in the controller like this: Controller { function __construct() { // Call the Model constructor parent::__construct(); $this->load->database('dev'); $this->load->database('automation_dashboard'); } function 1(){ $this->db->modelA->uses_DB_A(); $this->db->modelB>uses_DB_B(); } } MY database.php entries: $active_group = 'dev'; $active_record = TRUE; $db['dev']['hostname'] = 'localhost'; $db['dev']['username'] = 'sa'; $db['dev']['password'] = 'pass!'; $db['dev']['database'] = 'mydomain'; $db['dev']['dbdriver'] = 'mysql'; $db['dev']['dbprefix'] = ''; $db['dev']['pconnect'] = TRUE; $db['dev']['db_debug'] = TRUE; $db['dev']['cache_on'] = FALSE; $db['dev']['cachedir'] = ''; $db['dev']['char_set'] = 'utf8'; $db['dev']['dbcollat'] = 'utf8_general_ci'; $db['dev']['swap_pre'] = ''; $db['dev']['autoinit'] = TRUE; $db['dev']['stricton'] = FALSE; $db['dev_dashboard']['hostname'] = 'localhost'; $db['dev_dashboard']['username'] = 'sa'; $db['dev_dashboard']['password'] = 'password'; $db['dev_dashboard']['database'] = 'mydomain2'; $db['dev_dashboard']['dbdriver'] = 'mysql'; $db['dev_dashboard']['dbprefix'] = ''; $db['dev_dashboard']['pconnect'] = TRUE; $db['dev_dashboard']['db_debug'] = TRUE; $db['dev_dashboard']['cache_on'] = FALSE; $db['dev_dashboard']['cachedir'] = ''; $db['dev_dashboard']['char_set'] = 'utf8'; $db['dev_dashboard']['dbcollat'] = 'utf8_general_ci'; $db['dev_dashboard']['swap_pre'] = ''; $db['dev_dashboard']['autoinit'] = TRUE; $db['dev_dashboard']['stricton'] = FALSE; /* End of file database.php */ /* Location: ./application/config/database.php */
[eluser]jwright[/eluser]
Hi, two things I see you might want to look into... Review the section "Connecting to Multiple Databases" of the docs here http://ellislab.com/codeigniter/user-gui...cting.html (it shows exactly how to do it). Also, I see you are loading 'automation_dashboard' but you have no config for it. The config is only for 'dev' and 'dev_dashboard' . Hope this helps
[eluser]Unknown[/eluser]
Hi jwright, I've done both things and it still uses the first db (dev) and not dev_dashboard.
[eluser]Bhashkar Yadav[/eluser]
please post your code within "code" tag .... |
Welcome Guest, Not a member yet? Register Sign In |