Kohana 3.0 有一个很强壮的数据库模块。默认情况下数据库模块支持 MySQL 和 PHP-PDO 驱动
数据库模块已经包含在了 Kohana 3.0 安装程序之中,但是还需要在使用之前启动它。在你的 application/bootstrap.php 文件里修改 Kohana::modules() 方法中 database 模块,就像下面这样。
Kohana::modules(array(
‘userguide’ => MODPATH.’userguide’,
‘database’ => MODPATH.’database’, // Database access
‘pagination’ => MODPATH.’pagination’,
));
当模块启动以后,你还需要提供一个配置文件来使模块知道如何连接到你的数据库。你能在 modules/database/config/database.php 中找到一个配置文件的示例。复制这个配置文件到你的应用层
cp -a modules/database/config/database.php application/config/database.php
展开配置文件并且为你的数据库连接做一些必要的修改。下列的示例文件展示了2个mysql连接。你能定义许多你所需要的数据库连接,但是你必须确定有一个连接叫 default
return array
('default' => array('type' => 'mysql','connection' => array('hostname' => 'localhost','username' => 'dbuser','password' => 'mypassword','persistent' => FALSE,'database' => 'my_db_name',),'table_prefix' => '','charset' => 'utf8','caching' => FALSE,'profiling' => TRUE,),'remote' => array('type' => 'mysql','connection' => array('hostname' => '55.55.55.55','username' => 'remote_user','password' => 'mypassword','persistent' => FALSE,'database' => 'my_remote_db_name',),'table_prefix' => '','charset' => 'utf8','caching' => FALSE,'profiling' => TRUE,),
);
MySQL 数据库能接受下面的连接配置选项
■ 字符串的主机名 hostname *端口和套接字可以添加到主机名
例如:localhost:3306
■ 字符串的套接字 socket
■ 字符串的用户名 username
■ 字符串的密码 password
■ 布尔值的持久链接 persistent
■ 字符串的数据库名 database
PDO 数据库能接受下列这些连接配置选项
■ 字符串的数据源 dsn
■ 字符串的用户名 username
■ 字符串的密码 password
■ 布尔值的持久链接 persistent
■ 字符串的标识符 identifier