Welcome Guest, Not a member yet? Register   Sign In
How can I manage mutiple databases?
#1

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-gu...cting.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
Reply
#2

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(); 
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB