CodeIgniter Forums

Full Version: Active Record + Multiple Databases + Models
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]lookatthosemoose[/eluser]
Hiya everybody!

Question in regards to Active Record / Multiple Databases and their use in Models.


OK, so I've created 2 database connection groups in my DB config file.

Next, I (successfully) connect to the DBs in a controller like such:

Code:
function Login()
{
  parent::Controller();
  $MATRIX = $this->load->database('matrix',TRUE);
  $CAMPAIGN =  $this->load->database('campaign',TRUE);
}

Later in my controller, I want to invoke the function of a model (using active record) like such: EDIT - I'm aware the model isn't loaded in the code above, that's part of the question

Code:
function process(){
$code = $this->code->create();
}

"code" being the (active record) model where I want to run the query, like such:

Code:
class Code extends Model {


    function Code(){
        // Call the Model constructor
        parent::Model();
    }
    function create(){
        $query = $CAMPAIGN->get('codes',1);
    }
}

Initially, I was auto-loading my models in my autoload.php, which now I see I cannot do when using multiple DBs because (I think) I have to pass in the DB info when the model is loaded, either using this from what I found in the Docs:
Code:
$this->load->model('code', '', TRUE);   //autoload (I dont think this will work)

OR

Code:
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;

$this->load->model('code', '', $config);

So 2 main questions:
1 - How would I access one of the DB connections from within my models.
2 - Without having to use the 2nd option above of passing in the DB config array?

I figure since it's already loaded as $CAMPAIGN and $MATRIX (above), I should be able to tell the model to use one of those. I hope this makes sense. Thanks!

--Eric--