0

我对 Kohana 3.3 和使用不同的数据库配置有疑问。我有一个带有“默认”配置和“其他”的 config/database.php,如下所示:


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 仍将使用“默认”配置。我究竟做错了什么?

谢谢!

4

2 回答 2

1

如果您想通过 kohana 更改当前使用的连接,请尝试以下操作:

Database::$default = 'other';

从这一行开始,您的代码将使用“其他”连接,直到您使用相同的方式再次将其切换为“默认”。

您还可以在以简单的方式执行查询时使用另一个数据库配置:

DB::...->execute('other');

或者,如果您之前存储了数据库实例:

$other = Database::instance('other');
DB::...->execute($other);

通过...我的意思是您的查询。

于 2014-08-27T10:29:10.027 回答
0

您需要将连接存储在变量中,否则default将使用连接。

文档

于 2014-08-26T07:45:49.040 回答