不错,又学了一个新的方法
几点想法:
1、不能自动切换运行环境,比如用svn来管理代码,要对这个文件做一下特殊处理,比如忽略。
2、有时不止database.php,其他文件也需要量身定做,不是一个整体解决方案。
数据库多配置方案的实现
虽然我不懂 Ruby 和 ROR,不过在当初接触的时候就感觉 ROR 的多数据库配置是一个很棒的想法,之前由于忙碌没有来做,昨天看到 lzyy 在博客上面发布了自己的实现想法,感觉还是有些麻烦,今天自己做了另外一种方法:
1. 扩展 Kohana 数据库类(libraries/MY_Database.php)
class Database extends Database_Core {
/**
* Multiway profile of Database configures
*/
public function __construct()
{
$config = array();
// custom environment key to databse config.
$environment = Kohana::config('database.environment');
// available: development, test, pro
if ( !is_array($environment) && !empty($environment) )
{
$config = Kohana::config('database.'.$environment);
}
parent::__construct($config);
}
}
2. 修改 config/databse.php
// Sets the default databse config to "development" $config['environment'] = 'development'; // original Kohana database config $config['default'] = array ( 'benchmark' => TRUE, 'persistent' => FALSE, 'connection' => array ( 'type' => 'mysql', 'user' => 'dbuser', 'pass' => 'p@ssw0rd', 'host' => 'localhost', 'port' => FALSE, 'socket' => FALSE, 'database' => 'database_name' ), 'character_set' => 'utf8', 'table_prefix' => '', 'object' => TRUE, 'cache' => FALSE, 'escape' => TRUE ); // Development $config['development'] = array ( 'benchmark' => TRUE, 'persistent' => FALSE, 'connection' => array ( 'type' => 'mysql', 'user' => 'dbuser', 'pass' => 'p@ssw0rd', 'host' => 'localhost', 'port' => FALSE, 'socket' => FALSE, 'database' => 'database_name' ), 'character_set' => 'utf8', 'table_prefix' => '', 'object' => TRUE, 'cache' => FALSE, 'escape' => TRUE ); // Test online $config['test'] = array ( 'benchmark' => TRUE, 'persistent' => FALSE, 'connection' => array ( 'type' => 'mysql', 'user' => 'dbuser', 'pass' => 'p@ssw0rd', 'host' => 'localhost', 'port' => FALSE, 'socket' => FALSE, 'database' => 'database_name' ), 'character_set' => 'utf8', 'table_prefix' => '', 'object' => TRUE, 'cache' => FALSE, 'escape' => TRUE ); // Production online $config['production'] = array ( 'benchmark' => TRUE, 'persistent' => FALSE, 'connection' => array ( 'type' => 'mysql', 'user' => 'dbuser', 'pass' => 'p@ssw0rd', 'host' => 'localhost', 'port' => FALSE, 'socket' => FALSE, 'database' => 'database_name' ), 'character_set' => 'utf8', 'table_prefix' => '', 'object' => TRUE, 'cache' => FALSE, 'escape' => TRUE );
$config['default'] 数组并没有移除,这是为了保证配置在默认情况下不报错。
同时我已经把此功能提交到了官方,希望在今后的版本可以采用:http://dev.kohanaphp.com/issues/1915
Source:
MY_Database.php:http://code.google.com/p/kohana-fans-cn/source/browse/trunk/libraries/MY_Database.php
config/database.php:http://code.google.com/p/kohana-fans-cn/source/browse/trunk/config/database.php
不错,又学了一个新的方法
几点想法:
1、不能自动切换运行环境,比如用svn来管理代码,要对这个文件做一下特殊处理,比如忽略。
2、有时不止database.php,其他文件也需要量身定做,不是一个整体解决方案。
在官网提交之后,发现还有更为简单的方案被提出,而且这个需求已经纳入官方考虑范围内,期待。
自动环境切换要付出额外的效率,产品上线后每每执行前先对一个确定的环境来个假设,何必呢,这一点我还是倾向手动配置一下。
@Yahasana
嗯,不过一个判断,差的了那么多么?如果这样嫌影响效率的话,可以考虑这个:
$environment = 'development';
$config['development'] = array
(
'type' => 'mysql',
'connection' => array()
.....
);
$config['production'] = array
(
'type' => 'mysql',
'connection' => array()
.....
);
// Change default depending on the environment setting
$config['default'] = $config[$environment];
// return $config; this is required for Kohana 3这个方案在单一数据库情况下可以采用,但是对多数据库,这就配不出来了。我现在做的系统,每个大的module都自己独立数据库。
发表讨论