Welcome Guest, Not a member yet? Register   Sign In
Active Record + Multiple Databases + Models

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:

function Login()
  $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

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

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

class Code extends Model {

    function Code(){
        // Call the Model constructor
    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:
$this->load->model('code', '', TRUE);   //autoload (I dont think this will work)


$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!


Theme © iAndrew 2016 - Forum software by © MyBB