简体   繁体   English

获取当前数据库名称-cakephp 3

[英]Get current database name - cakephp 3

I need to get current and default DB name in cakephp 3. 我需要在cakephp 3中获取当前和默认的数据库名称。

I know how its possible in cakephp 2 but cannot get a work around for cakephp 3. I have looked into the cakephp library in datasource/connectionManager.php but still i cant get any method to use. 我知道它在cakephp 2中是如何可能的,但是无法解决cakephp 3的问题。我已经研究了datasource / connectionManager.php中的cakephp库,但仍然无法使用任何方法。 Can you please help me out in finding out the current connections config details? 您能帮我找出当前的连接配置详细信息吗? Thanks in advance. 提前致谢。

I found my answer after looking into some of the core cakephp files. 在查看了一些核心cakephp文件后,我找到了答案。 Plus ADmad's answer gave me a hint. 再加上ADmad的回答给了我一个提示。 I found two ways of doing it. 我发现了两种方法。

Method 1: Using Current model Object. 方法1:使用当前模型对象。

$this->{$modelName}->connection()->config();

will give config, and, 将提供配置,并且,

$this->{$modelName}->connection()->config()['database'];

will give the current Database name. 将给出当前的数据库名称。

Method 2: Using Datasource Object. 方法2:使用数据源对象。

$dataSourceObject = ConnectionManager::get($connectionName); // $connectionName can be 'default'

Config: $dataSourceObject->config(); 配置: $dataSourceObject->config();

Current Database name: $dataSourceObject->config()['database']; 当前数据库名称: $dataSourceObject->config()['database'];

If you're on older version of CakePHP (mine is 2.4.6), you can also use: 如果您使用的是CakePHP的旧版本(我的版本是2.4.6),则还可以使用:

ConnectionManager::$config->default['database']

and you can load ConnectionManager via: 您可以通过以下方式加载ConnectionManager:

App::uses('ConnectionManager', 'Model');

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

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