如果从Kohana 3.1控制器运行以下位代码
$query = DB::select("select * from foo");
$results = $query->execute();
foreach($results as $result)
{
var_dump($result);
}
Kohana将尝试使用application / config / database.PHP返回的数组中的信息连接到数据库.具体来说,如果将使用默认组中设置的信息.
return array
(
'default' => array
(
'type' => 'MysqL',
'connection' => array(
/**
* The following options are available for MysqL:
*
* string hostname server hostname, or socket
* string database database name
* string username database username
* string password database password
* boolean persistent use persistent connections?
*
* Ports and sockets may be appended to the hostname.
*/
'hostname' => 'localhost',
'database' => 'kohana',
'username' => FALSE,
'password' => FALSE,
'persistent' => FALSE,
但是,此配置数组接受多个顶级项(我认为称为db-groups).我怎么能/应该告诉Kohona 3.1使用在非默认db-group中设置的信息进行连接和查询.
解决方法:
您可以将数据库组作为execute的参数传递
查看源代码:Line 201 of classes/kohana/database/query.php和Database::instance()
$this->execute('group');
您还可以编写以$query = Database :: instance(‘group’)开头的查询