Welcome Guest, Not a member yet? Register   Sign In
Making Multiple Database Connections "Global"
#1

[eluser]IanMcQ[/eluser]
Hi everybody,

I am developing an application that requires connections to two different databases. What I'm doing is instantiating these connections in MY_Controller so that they are available to all inheriting controllers.

Here's MY_Controller.php

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    // Declare
    private $CI;
    private $db;
    protected $maindb;

    function __construct()
    {
     // Do parent
        parent::__construct();

        // Load them in
        $this->db = $this->load->database('default', TRUE);
        $this->maindb = $this->load->database('maindb', TRUE);
    }

}
I'm running into a problem... both of my database instances (namely, just "maindb") is not accessible to different areas like my Libraries or Helpers. How can I make these instances accessible to all areas of CI? Is there an easier way? Hooks?

Any help is appreciated. Thanks in advanced.
#2

[eluser]Aken[/eluser]
Declare the properties as public.
#3

[eluser]IanMcQ[/eluser]
Are there any security risks in that?
#4

[eluser]Aken[/eluser]
No.
#5

[eluser]IanMcQ[/eluser]
Changed both variables in MY_Controller to PUBLIC. It now reads:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    // Declare
    public $db;
    public $maindb;

    function __construct()
    {
     // Do parent
        parent::__construct();

        // Load them in
        $this->db = $this->load->database('default', TRUE);
        $this->maindb = $this->load->database('maindb', TRUE);
    }

}

Still having troubles accessing $this->maindb in a Library:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sess
{
private $CI;

// --------------------------------------------------------------------

function __construct()
{
  // --------------------------------------
  // Get instance, do logging
  // --------------------------------------
  
  $this->CI =& get_instance();
  log_message('debug', 'TDN-specific Session Class Initialized');
  
    var_dump($this->CI->maindb); // == RETURNS NULL
  
  [...]

var_dump returns NULL of $this->CI->maindb, which should be the maindb instance. Any ideas?

UPDATE: $this->CI->db seems to be working fine, for whatever reason. Have a feeling it has to do with the fact that it's the default anyway, and is wired for use in the application anywhere already.
#6

[eluser]Aken[/eluser]
Did you check if the load->database() call is actually returning something?
#7

[eluser]IanMcQ[/eluser]
Alright, $this->maindb is now working in Controllers, but how do I pass them to Libraries?

When I create my own library, in the __construct method, I have:

Code:
$this->CI =& get_instance();
var_dump($this->CI->maindb);

and $this->CI->maindb returns NULL. Any ideas?

Or do I have to load in the maindb instance in my Libraries manually? I thought it would pass through the instance though... unless MY_Controller is hooked after Libraries are loaded.




Theme © iAndrew 2016 - Forum software by © MyBB