全局更改CakePHP数据库配置 [英] Change CakePHP database configuration globally

查看:171
本文介绍了全局更改CakePHP数据库配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用于CakePHP项目的数据库,分别命名为projectproject_dev.数据库project用于生产中,因为从版本3开始,CakePHP删除了表前缀选项,所以我选择使用两个分离的数据库.

I have two databases used for my CakePHP project, respectively named project and project_dev. The database project is used in production and because since the version 3, the CakePHP remove the table prefix option, I choose to use two separated databases.

从我的/config/app.php中提取:

Extract from my /config/app.php :

'Datasources' => [
    'dev' => [...],
    'prod' => [...]
]

问题是我需要使用dev数据库...进行开发;所以我需要告诉Cake在开发过程中默认使用dev配置.

The problem is that I need to use the dev database... for development; so I need to tell Cake to use the dev configuration by default during development.

为此,我尝试创建一个AppTable.php,这是我所有* Table.php文件所继承的类,并具有以下功能:

To do that, I tried to create an AppTable.php, a class inherited by all my *Table.php files, with the following function :

/**
 * Define the connection name based on the debug config value
 * @return string the connection name
 */
public static function defaultConnectionName(): string {
    return Configure::read('debug') ? 'dev' : 'prod';
}

一些任务(例如单元测试)的问题,Cake正在尝试使用default数据库配置.

The problem for a few tasks, such as unit tests, Cake is trying to use the default database config.

我担心我的AppTable.php黑客效率不高,我认为直接在app.php中像这样调整加载时的配置:

I'm afraid that my AppTable.php hack is not efficient and I think that tweaking the configuration on load directly in app.php like this :

$config = [
    [...],
    'Datasources' => [
        'dev' => [...],
        'prod' => [...]
    ],
    'UseDatasourcesConfig' => 'dev',
    [...],
];

$config['Datasources']['default'] = $config['Datasources']['UseDatasourcesConfig'];

return $config;

...听起来也像黑客一样.

...sounds, like a hack too.

第二种方法可行吗?还是存在一种更正式/更正确的方式来全局设置数据库配置?

Is this second method viable? Or does a more formal/proper way to set the database config globally exist?

推荐答案

根据环境提供配置是一项非常常见的任务,并且还有很多其他方法可以解决此问题.

Providing configuration depending on the environment is a pretty common task, and there's quite a few other ways to handle this.

如果环境允许,则可以使用环境变量.您可以通过DATABASE_URL变量以DSN连接字符串之类的

In case the environment allows it, you could use environment variables. You could easily provide DB configuration via the DATABASE_URL variable in form of a DSN connection string like

mysql://user:pass@localhost/production?encoding=utf8&timezone=UTC&cacheMetadata=true

食谱>配置>环境变量

Cookbook > Configuration > Environment Variables

另一种选择是根据当前环境加载单独的/其他配置文件,以便您只能部署特定环境实际需要的那些文件.

Another option is to load separate/additional config files based on the current environment, so that you can deploy only those files that are actually required for a specific environment.

Configure::load('app', 'default', false);
// $enviroment needs to be validated /sanitzied in order to
// avoid loading arbitrary files from the config directory
Configure::load($enviroment, 'default', true);

食谱>配置>加载其他配置文件

Cookbook > Configuration > Loading Additional Configuration Files

还有连接别名,CakePHP在测试环境中自动完成.假设您有一个default和一个production配置,然后可以在生产环境中将production连接别名为default,这样在您的代码尝试使用default连接的任何地方,它实际上都会使用production一个.

And there's also connection aliasing, CakePHP does that automatically in the test environment. Say you have a default and a production confiuration, you could then alias the production connection as default in your production environment, so that everywhere your code tries to use the default connection, it will actually use the production one.

\Cake\DataSource\ConnectionManager::alias('production', 'default');

API> \ Cake \ DataSource \ ConnectionManager :: alias()

API > \Cake\DataSource\ConnectionManager::alias()

这篇关于全局更改CakePHP数据库配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆