Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current database name - cakephp 3

Tags:

php

cakephp

I need to get current and default DB name in 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. Can you please help me out in finding out the current connections config details? Thanks in advance.

like image 841
Ashish Choudhary Avatar asked Oct 19 '15 11:10

Ashish Choudhary


1 Answers

I found my answer after looking into some of the core cakephp files. Plus ADmad's answer gave me a hint. I found two ways of doing it.

Method 1: Using Current model Object.

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

will give config, and,

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

will give the current Database name.

Method 2: Using Datasource Object.

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

Config: $dataSourceObject->config();

Current Database name: $dataSourceObject->config()['database'];

like image 79
Ashish Choudhary Avatar answered Oct 30 '22 08:10

Ashish Choudhary