2011-10-24 38 views
2

我有一個多租戶應用程序,我正在開發,我快到了。Codeigniter數據庫 - 自動選擇

目前我使用一個數據庫,每個客戶 - 每一次複製它,並命名根據基於$ _ SERVER [「HTTP_HOST」] ...

了很多表的每個數據庫永遠不會改變的,並且包含相同信息。

我想有一個主要的應用程序數據庫,其中包含這些信息,包括客戶端列表和什麼數據庫被稱爲。這個數據庫然後將用於選擇正確的客戶數據庫...

這是可能的,任何人都可以指向我的方向適當的教程?

感謝

回答

1

OK,所以你需要爲你的「行政」數據庫和「客戶」數據庫不同的數據庫CONFIGS。

使用的例子,從這個頁面生成這些單獨CONFIGS:

http://ellislab.com/codeigniter/user_guide/database/configuration.html

然後,在你的主控制器(請告訴我你的延伸MY_Controller.php而不是僅僅使用CI_Controller.php),你」我想讀你的'管理員'數據庫並獲取所有客戶信息。然後,您將要生成$config陣列爲這個客戶數據庫,並與新的$config陣列重裝數據庫:

// in MY_Controller constructor 

// do your thing here to connect to the admin database and get client details 
// ... 
// 

$config['hostname'] = "localhost"; 
$config['username'] = $client_db_username; // you got this from the 'admin' database 
$config['password'] = $client_db_password; // you got this from the 'admin' database 
$config['database'] = $client_db_name; // you got this from the 'admin' database 
$config['dbdriver'] = "mysql"; 
$config['dbprefix'] = ""; 
$config['pconnect'] = FALSE; 
$config['db_debug'] = TRUE; 
$config['cache_on'] = FALSE; 
$config['cachedir'] = ""; 
$config['char_set'] = "utf8"; 
$config['dbcollat'] = "utf8_general_ci"; 

$this->load->database($config); 

然後您就必須在客戶端數據庫的連接。

注意 如果您需要連接到這兩個數據庫來回,然後就可以連接到多個數據庫,因爲這頁解釋:http://ellislab.com/codeigniter/user_guide/database/connecting.html

+0

完美,我只是通過你的答案閱讀和包含在內的所有鏈接。看起來這正是我所需要的... –