2017-02-08 65 views
0

database.php中根據登錄用戶的憑據動態切換到不同的數據庫?

<?php 
class Database extends CI_Controller { 
    public $branch_db; 

    function __construct($company_name, $branch_name) { 
     parent::__construct(); 
     $branch_db = $this->load->database($company_name.'_db_'.$branch_name); 
    } 
} 
?> 

Account_model.php

protected function verifyLogin($username, $password) { 
    $this->db->trans_start(); 

    $sql = "SELECT password, company_id, branch_id 
       FROM account 
       WHERE username = {$username}"; //Prepare statement 

    $query = $this->db->query($sql); 
    $result_num = $query->num_rows(); 

    if ($result_num == 1) { 
     $first_row = $query->row(); 
     $stored_password = $first_row->password; 

     if (password_verify($password, $stored_password)) { 
      //Successful login 
      //Get company name 
      //Get branch name 
      //Set database 
      //Pass company_name, branch_name 
      //All models must be able to access the dynamically loaded database 
     } 
    } 
    $this->db->trans_complete(); 
} 

有可以訪問多個數據庫。每個分支都有一個動態創建的數據庫。每個分支都有一個帳戶。登錄賬戶時,將訪問與該賬戶相關的數據庫。該數據庫將被用於整個會話,直到用戶註銷。

我有一個主數據庫,其中存儲有效用戶名/密碼組合的主列表。我在config文件夾中的database.php中聲明。但是在用戶登錄後,我需要切換到與該用戶帳戶相關的數據庫,以便他們可以訪問他們的數據。

我怎樣才能使用這個控制器爲我的模型,使他們都訪問相同的數據庫(無需重複$this->load->database()反覆)?

+0

手段?你想在模型的每個函數上加載數據庫? –

+0

是的,我想呢?就像,我有點想讓這個db等價於'$ this-> db',讓它可以全局訪問所有模型(每個數據庫都有相同的表結構)@JayminsFakeAccount – herondale

回答

0

在你的database.php中Application->config->database.php 我期待你創建一個會話,存儲admin_type變量,因爲它登錄。

$admin_type = $this->session->userdata['admin_type']; 
$active_group = 'default'; 
$query_builder = TRUE; 
if ($admin_type==1) { 
    $db['default'] = array(
     'dsn' => '', 
     'hostname' => 'localhost', 
     'username' => 'root', 
     'password' => 'root', 
     'database' => 'name_db', 
     'dbdriver' => 'mysqli', 
     'dbprefix' => '', 
     'pconnect' => FALSE, 
     'db_debug' => (ENVIRONMENT !== 'production'), 
     'cache_on' => FALSE, 
     'cachedir' => '', 
     'char_set' => 'utf8', 
     'dbcollat' => 'utf8_general_ci', 
     'swap_pre' => '', 
     'encrypt' => FALSE, 
     'compress' => FALSE, 
     'stricton' => FALSE, 
     'failover' => array(), 
     'save_queries' => TRUE 
    ); 
}else{ 
    $db['default'] = array(
     'dsn' => '', 
     'hostname' => 'localhost', 
     'username' => 'root', 
     'password' => 'root', 
     'database' => 'name_db2', 
     'dbdriver' => 'mysqli', 
     'dbprefix' => '', 
     'pconnect' => FALSE, 
     'db_debug' => (ENVIRONMENT !== 'production'), 
     'cache_on' => FALSE, 
     'cachedir' => '', 
     'char_set' => 'utf8', 
     'dbcollat' => 'utf8_general_ci', 
     'swap_pre' => '', 
     'encrypt' => FALSE, 
     'compress' => FALSE, 
     'stricton' => FALSE, 
     'failover' => array(), 
     'save_queries' => TRUE 
    ); 
} 
+0

有多個數據庫可以被訪問。登錄後,根據用戶的分支和公司,將訪問該特定數據庫。我怎樣才能做到這一點?無需在我的模型中反覆執行'$ this-> load-> database()'? – herondale

+0

我猜登錄後會有鏈接改變吧?你可以這樣做,如果編碼也讓我編輯相同的代碼。 –

+0

我編輯了你可以看到的代碼,並且可以符合你的要求 –

相關問題