[英]Kohana 3.3 Database::instance('name') not working
我在Kohana 3.3上存在問題,並使用了不同的數據庫配置。 我有一個config / database.php和'default'config和'other'像這樣:
return array
(
'default' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'database-one',
'username' => 'root',
'password' => 'password',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
'other' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'database-two',
'username' => 'root',
'password' => 'password',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
));
但是在控制器或模型中嘗試使用時:
Database::instance('other');
Kohana仍將使用“默認”配置。 我究竟做錯了什么?
謝謝!
如果您想更改kohana當前使用的連接,請嘗試以下操作:
Database::$default = 'other';
從這一行開始,您的代碼將使用“其他”連接,直到您以相同的方式將其再次切換為“默認”為止。
以簡單的方式執行查詢時,也可以使用另一種數據庫配置:
DB::...->execute('other');
或者,如果您之前存儲了數據庫實例:
$other = Database::instance('other');
DB::...->execute($other);
我的意思是您的查詢。
您需要將連接存儲在變量中,否則將使用default
連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.