[eluser]bojack[/eluser]
I found a convenient solution for dealing with multiple databases, and since I have seen a few posts concerning this I thought I would share.
function _assign_libraries()
{
if ($CI =& get_instance())
{
$this->lang = $CI->lang;
$this->load = $CI->load;
// use CodeIgniter's db if not set by model
if (empty($this->db))
{
$this->db = $CI->db;
}
$this->config = $CI->config;
}
}
Then, in your model you can do:
Code:
public function __construct()
{
$CI =& get_instance();
$this->db = $CI->load->database('other_database_key', TRUE);
parent::__construct();
}
Now you can use the models in your controller and not have to worry about unsetting and resetting $this->db, or caring where the database is.
This will of course fail all over if you are trying to relate an object from one DB to an object from another (I assume). But for my needs, it seems like the easy way to deal with a script that needs to read from multiple databases.