Question

So far I really enjoy how CodeIgniter plays together with PHP ActiveRecord ORM tool,

How would I go about specifying which connection to use in my models?

My config/database.php file has 2 connections set up: $db['default'] and $db['myOtherDb']

Thanks

Était-ce utile?

La solution 3

UPDATE:

I found my solution by invoking the autocomplete in my IDE inside my ActiveRecord Model and checking the available methods/properties, one of the properties is $connection and we can set it to the connection that we want to use, so in my model:

public static $connection = 'byOtherDb';

Autres conseils

Inside the controller you can add :

var $db_1, $db_2;
function __construct()
{
    parent::__construct();

    $db1_param = 'mysql://database_username:password@localhost/database_name?char_set=utf8&dbcollat=utf8_general_ci';
    $db2_param = 'mysql://database_username:password@localhost/database_name?char_set=utf8&dbcollat=utf8_general_ci';

    $this->db_1 = $this->load->database($db1_param, TRUE);
    $this->db_2 = $this->load->database($db2_param, TRUE);
}

then your handlers are : $this->db_1 , $this->db_2

Specify the new db name in the load->database function,

$otherdb = $this->load->database('myotherdb', TRUE);

Refer this for more info

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top