1

I've added the appropriate configuration arrays to database.php and they work, however, I would like an easier way to access the different databases. Right now I have to do something like this in every controller method:

function index(){
    $BILLING = $this->load->database('billing', TRUE);
    $INVENTORY = $this->load->database('inventory', TRUE);

    $data['billing'] = $BILLING->get('stuff');
    $data['inventory'] = $INVENTORY->get('stuff');
}

I'd like to be able to put those first two lines in some sort of before filter or pre_controller hook.

Dex
  • 12,527
  • 15
  • 69
  • 90

1 Answers1

8

You could simply load the database instances globally in your constructor, then they would be available to all controller methods...

example controller

class Example extends CI_Controller {

    //declare them globally in your controller
    private $billing_db;
    private $inventory_db;

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

        //Load them in the constructor
        $this->billing_db = $this->load->database('billing', TRUE);
        $this->inventory_db = $this->load->database('inventory', TRUE);
    }

    function index() {

        //Then use them in any controller like this
        $data['billing'] = $this->inventory_db->get('stuff');
        $data['inventory'] = $this->billing_db->get('stuff');

    }

}

And if these same databases are used across multiple controllers, you might consider extending the base controller to include these global variables and load them in the constructor of your base controller in MY_Controller.php

example MY_Controller.php

class DB_Controller extends CI_Controller {

    //declare them globally in your controller
    private $billing_db;
    private $inventory_db;

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

        //Load them in the constructor
        $this->billing_db = $this->load->database('billing', TRUE);
        $this->inventory_db = $this->load->database('inventory', TRUE);
    }

}

Then you'd use it like this...

class Example extends DB_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {

        //Then use them in any controller like this
        $data['billing'] = $this->inventory_db->get('stuff');
        $data['inventory'] = $this->billing_db->get('stuff');

    }

}
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • Across all controllers would be even better. Would you mind posting a snippet of how I'd extend CI_Controller? – Dex Feb 07 '11 at 05:54
  • This might be what you are looking for in terms of extending CI_Controller: http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY – JDM Feb 07 '11 at 08:36
  • Or even the CI documentation http://codeigniter.com/user_guide/general/core_classes.html ... You'd just put the logic that I put in my example controller, in your extended base controller. – jondavidjohn Feb 07 '11 at 13:27
  • @jandavidjohn - I didn't quite understand this usage. Generally, in a controller method, what we do is like this `$this->model_name->model_method('parameters');` and in the model method we have something like `$this->db->select('*');`- the active records methods I mean. In your snippet `$this->inventory_db->get('stuff');`, what is `get()`? Is it a method in model? If yes, then shouldn't `inventory_db` be the model name which it is not? Confused here. Please explain. – vikmalhotra Jun 28 '11 at 01:55
  • @ShiVik - No, `inventory_db` is not a model, it represents the `inventory` database configuration. So `$this->inventory_db->get('stuff')` is the same as `$this->db->get('stuff')` but is using the `inventory` database configuration. `->get('tablename')` is a built in method to `SELECT *` , see docs here ---> http://codeigniter.com/user_guide/database/active_record.html#select – jondavidjohn Jun 28 '11 at 13:51