2017-08-31 59 views
1

我有一些(模型)方法,它們分別連接到不同的數據庫。Codeigniter:具有不同數據庫連接的不同方法(多個數據庫連接)

我從database.php創建了兩個數據庫配置,將它們加載到模型中並創建了兩個方法;一個連接到DB1,另一個連接到DB2。 (下面的示例代碼)

當我更換由$ DB1$ DB2$這個 - >分貝,我得到這樣的錯誤:

Message: Undefined variable: DB1 // or DB2 

否則,我得到這個錯誤:

Message: Undefined property: Home::$db 

我試圖包括$ DB = $這個 - >負載>數據庫( 「DATABASE_NAME」,TRUE);每個方法中的連接到特定的數據庫。 它的工作原理但我知道它不是一個很好的練習,因爲我重複使用這個方法,所以再次連接它。

我真的很困惑。

下面是我的代碼:

database.php中

$db['db1'] = array(
    'dsn' => '', 
    'hostname' => 'localhost', 
    'username' => 'root', 
    'password' => '', 
    'database' => 'db1', 
    '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 
); 


$db['db2'] = array(
    'dsn' => '', 
    'hostname' => 'other_host', 
    'username' => 'root', 
    'password' => '', 
    'database' => 'db2', 
    'dbdriver' => 'mssql', 
    '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 
); 

型號:

function __construct() 
{ 
    parent::__construct(); 
    $DB1= $this->load->database("db1", TRUE); 
    $DB2= $this->load->database("db2", TRUE); 
} 

public function get_from_db1($id) 
{ 
    // $query = $this->db->get_where("my_table1",array("ID"=>$id)); 
    $query = $DB1->get_where("my_table1",array("ID"=>$id)); 
    return $query->row_array(); 
} 


public function get_from_db2($id) 
{ 
    // $query = $this->db->get_where("my_table2",array("ID"=>$id)); 
    $query = $DB2->get_where("my_table2",array("ID"=>$id)); 
    return $query->row_array(); 
} 

另外:

  • 難道我宣佈和加載我的數據庫嗎?
  • 我應該在哪裏放$ this-> load-> database();在控制器或模型中使用函數?

請幫忙!

在此先感謝你們!

回答

3

你需要外界的你構造器

public $DB1; 
public $DB2; 

定義變量並調用它內部的構造函數

function __construct() 
{ 
parent::__construct(); 
$this->DB1= $this->load->database("db1", TRUE); 
$this->DB2= $this->load->database("db2", TRUE); 
} 

而且你的函數內部

public function get_from_db1($id) 
{ 
// $query = $this->db->get_where("my_table1",array("ID"=>$id)); 
$query = $this->DB1->get_where("my_table1", array("ID" => $id)); 
return $query->row_array(); 
} 


public function get_from_db2($id) 
{ 
// $query = $this->db->get_where("my_table2",array("ID"=>$id)); 
$query = $this->DB2->get_where("my_table2", array("ID" => $id)); 
return $query->row_array(); 
} 
+0

先生,這是真的所有DB1變量聲明和方法?或者只是一個錯字? – Jorz

+0

這是一個錯字!更正。 – Saty

+0

謝謝!它現在有效!經過四周尋找,終於做到了...... – Jorz

1

你加載主數據庫類在autoload.php中:

$autoload['libraries'] = array('database'); 

您需要加載此庫才能使用$db屬性。

+0

謝謝先生。已經更新了我的自動加載。 – Jorz