• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where to define database when not autoloading?

#1
Hi.

I have 2 databases and I want to optimize my site.

Should I call

PHP Code:
$this->db1 $this->load->database('somegroupname1'true);
$this->db2 $this->load->database('somegroupname2'true); 

From the Controller or from the Model ?


I am worried that my controller loading 3 to 5 models should I call the database from the controller or from the model.  Some models use 1 database and some use another.  Some models do not require any models and some only require 1 database connection.

I am trying to reduce database connections..
Thanks
Reply

#2
You could extend the loader and add 2 methods to connect to your databases (along with two properties in the super object to reference them). The easiest way to do so would be to create a "MY_Loader" class and copy the database() method from the CI loader into your loader, then refactor each copy to use unique method names and reference unique properties on the $CI instance. In other words, something like this:

PHP Code:
<?php 

class MY_Loader extends CI_Loader
{
    public function 
__construct()
    {
        
parent::__construct();
    }

    public function 
databaseOne($params ''$return FALSE$query_builder NULL)
    {
        
// Grab the super object
        
$CI =& get_instance();

        
// Do we even need to load the database class?
        
if ($return === FALSE && $query_builder === NULL && isset($CI->db1) && is_object($CI->db1) && ! empty($CI->db1->conn_id))
        {
            return 
FALSE;
        }

        require_once(
BASEPATH.'database/DB.php');

        if (
$return === TRUE)
        {
            return 
DB($params$query_builder);
        }

        
// Initialize the db variable. Needed to prevent
        // reference errors with some configurations
        
$CI->db1 '';

        
// Load the DB class
        
$CI->db1 =& DB($params$query_builder);
        return 
$this;
    }

    public function 
databaseTwo($params ''$return FALSE$query_builder NULL)
    {
        
// Grab the super object
        
$CI =& get_instance();

        
// Do we even need to load the database class?
        
if ($return === FALSE && $query_builder === NULL && isset($CI->db2) && is_object($CI->db2) && ! empty($CI->db2->conn_id))
        {
            return 
FALSE;
        }

        require_once(
BASEPATH.'database/DB.php');

        if (
$return === TRUE)
        {
            return 
DB($params$query_builder);
        }

        
// Initialize the db variable. Needed to prevent
        // reference errors with some configurations
        
$CI->db2 '';

        
// Load the DB class
        
$CI->db2 =& DB($params$query_builder);
        return 
$this;
    }


Then you would use it like this:

PHP Code:
$this->load->databaseOne();
$this->db1->query($sql1);

$this->load->databaseTwo();
$this->db2->query($sql2); 

The loader would then manage the connection(s), returning the open connection if it already exists.

With some extra work, it's probably possible to do this with less code, but this is the easiest/simplest solution that came to mind.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.