简体   繁体   English

Kohana 3.3数据库:: instance('name')不起作用

[英]Kohana 3.3 Database::instance('name') not working

I have an issue with Kohana 3.3 and using different database configurations. 我在Kohana 3.3上存在问题,并使用了不同的数据库配置。 I have a config/database.php with 'default' config and 'other' like this: 我有一个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,
));

But in a Controller or Model when trying to use: 但是在控制器或模型中尝试使用时:

 Database::instance('other'); 

Kohana will still use the 'default' configuration. Kohana仍将使用“默认”配置。 What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

If you would like to change currently used connection by kohana try this: 如果您想更改kohana当前使用的连接,请尝试以下操作:

Database::$default = 'other';

From this line your code will use 'other' connection till you will switch it again to 'default' using same way. 从这一行开始,您的代码将使用“其他”连接,直到您以相同的方式将其再次切换为“默认”为止。

You can also use another DB configuration once when executing the query in simple way: 以简单的方式执行查询时,也可以使用另一种数据库配置:

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

Or if you store your DB instance earlier: 或者,如果您之前存储了数据库实例:

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

By ... I mean your query. 我的意思是您的查询。

You need to store the connection in a variable, or the default connection will be used. 您需要将连接存储在变量中,否则将使用default连接。

Documentation 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM