CodeIgniter Forums
a little advice in connecting to 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: a little advice in connecting to multiple database (/showthread.php?tid=55121)



a little advice in connecting to multiple database - El Forum - 10-11-2012

[eluser]xeroblast[/eluser]
i had already checked the previous post regarding this matter and this ( http://ellislab.com/forums/viewthread/224254/ ) is the nearest answer to an advice i wanted.

my current set-up :
my db1 is access through the default connection. the users listed in db1 have different settings in connecting to the database. e.i. user1 db = db2 while user2 db = db3.. user1 can only see db1 & db2 while user2 can only see db1 & db3. db2 & db3 have the same tables and fields. so i want to use the same model in different connection without to call the config setting in every model i load.

controller: current and working
Code:
function index() {
$this->load->model('user_model');
$this->load->model('other_model');
$data['somedata'] = $this->user_model->get_data();
$data['otherdata'] = $this->other_model->get_data();
$this->load->view('html', $data);
}
user_model: current and working
Code:
var user_db;
function __construct() {
$config = $this->db1_model->get_user_db_configuration(); // return the config array and autoloaded
$this->user_db = $this->load->database( $config, true );
}
function somefunction() {
$query = $this->user_db->get('sometable');
return $query->result();
}
other_model: current and working
Code:
var user_db;
function __construct() {
$config = $this->db1_model->get_user_db_configuration(); // return the config array and autoloaded
$this->user_db = $this->load->database( $config, true );
}
function somefunction() {
$query = $this->user_db->get('sometable');
return $query->result();
}

my current problem is when loading multiple models in the controller which every model calls the same user configuration. i wanted to use the model to autoconnect to the database using via the 3rd parameter.
Code:
function index() {
$config = $this->db1_model->get_user_db_configuration();
$this->load->model('user_model','',$config);
$this->load->model('other_model','',$config);
...
}
but its not working. it is still using the default db configuration which is the db1.
i want it that so i only have to call
Quote:$this->db1_model->get_user_db_configuration()
only once.

thanks in advance.