CodeIgniter Forums
How can I manage mutiple databases? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How can I manage mutiple databases? (/showthread.php?tid=1218)



How can I manage mutiple databases? - PenelopeGlamour - 02-19-2015

Hi everybody!

I'm facing with manage 2 databases in my proyect, and I found a little help in the user-guide from ellislab https://ellislab.com/codeIgniter/user-guide/database/connecting.html but I don't know where I must to set this:
$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

Is it into the config/database.php file? or in my models? I also found some threads at stackoverflow, but the solutions point to the old forum, and... well, it is not more aviable Sad

so... any help?

thanks! Smile


PD English is not my mother language, I hope it will not be a issue! I can understand it really good, but with writting... I'm not soooo good


RE: How can I manage mutiple databases? - RobertSF - 02-19-2015

Yes, the documentation doesn't explain the entire process. It does tell you how to set up the two groups in config/database.php. You know that part. Now what you do is this.

In your model file, make it look like this
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


Class 
Some_model extends CI_Model {
  
    
public $db1;
 
   public $db2;

 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->db1 $this->load->database('database_1'TRUE);
 
       $this->db2 $this->load->database('database_2'TRUE);
 
     

Then, in your model functions, you do this
PHP Code:
public function get_records_1()
{
 
   return $this->db1->query('SELECT * FROM table_name;')
}

public function 
get_records_2()
{
 
   return $this->db2->query('SELECT * FROM table_name;')


In your controller, you don't have to do anything special. Just like normal.
PHP Code:
$records_1 $this->some_model->get_records_1();
$records_2 $this->some_model->get_records_2(); 



RE: How can I manage mutiple databases? - PenelopeGlamour - 02-20-2015

Ok! I will try this and comeback to say (I hope) that works to me. Thanks!