Welcome Guest, Not a member yet? Register   Sign In
Connect to multiple database server by type of query (delete, insert, select, update)
#2

The easiest way to do this is to create a MY_Model that handles this for you by creating 4 separate database connections, and then has basic CRUD methods that you use that use the correct individual connections. Something like the following (though this might not be the best performing example):

Code:
class MY_Model extends CI_Model {

    protected $db_select;
    protected $db_insert;
    protected $db_update;
    protected $db_delete;

    public function __construct()
    {
        $this->db_select = $this->load->database('select', true);
        $this->db_insert = $this->load->database('insert', true);
        $this->db_update = $this->load->database('update', true);
        $this->db_delete = $this->load->database('delete', true);
    }

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

    public function create($data)
    {
        return $this->db_insert->insert($data);
    }
    
    //--------------------------------------------------------------------
    
    // an so on...

}

Then in your database config file, you would create multiple groups with the appropriate names and connection details:

Code:
$db['insert'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'database_name',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => TRUE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'autoinit' => TRUE,
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array()
);
$db['delete'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'database_name',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => TRUE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'autoinit' => TRUE,
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array()
);
Reply


Messages In This Thread
RE: Connect to multiple database server by type of query (delete, insert, select, update) - by kilishan - 07-06-2015, 07:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB