문제

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

도움이 되었습니까?

해결책 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';

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top