-
chandara
Newbie
-
Posts: 1
Threads: 1
Joined: Jul 2015
Reputation:
0
I would like to ask one question with database connection in CodeIgniter 3.
I check in database configuration, it provide setting database connection values for specific environments by placing database.php such as:
PHP Code: $db['default'] = 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() );
But my requirement is separate query select, insert, delete and update to connect to server, for example, select.server.com, insert.server.com, delete.server.com and update.server.com respectively.
My question: how can I configure database connection depend one query type (insert, delete, update, select) in CodeIgniter 3?
Thanks.
-
kilishan
CI Project Lead
-
Posts: 1,462
Threads: 90
Joined: Oct 2014
Reputation:
120
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()
);
|