Kohana 3.3数据库::实例(';name';)不工作


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配置:

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

或者,如果您较早存储DB实例:

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

由。。。我是说你的问题。

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

文档