19

I need to know the database name and database server name inside a symfony project. How can one access the current database connection settings programmatically in symfony (using Doctrine)?

6 Answers 6

35

Assuming you have the EntityManager as $this->em

Get Doctrine Database Name from Symfony2:

$this->em->getConnection()->getDatabase();

Get Doctrine Host Name (Server Name) from Symfony2:

$this->em->getConnection()->getHost();

There are many other parameters you can access from the connection such as username, port and password. See the connection class for more info

1
  • 5
    Note that since May 2019 everything apart from getDatabase() has been deprecated, apparently without replacement: github.com/doctrine/dbal/issues/3580 so the other methods on this questions may be now just as valid.
    – jdog
    Sep 23, 2020 at 0:59
7

for example:

foreach(Doctrine_Manager::getInstance()->getConnections() as $connection){
  $conn = $connection->getOptions();
  preg_match('/host=(.*);/', $conn['dsn'], $host);
  var_dump($host);
}
1
2

The dbname in the dsn for a specific conexion:

databases.yml

all:
  conexion1:
    class: sfDoctrineDatabase
    param:
      dsn: 'mysql:host=localhost;dbname=basegestion1'
      username: miusuario
      password: ********
  conexion2:
    class: sfDoctrineDatabase
    param:
      dsn: 'mysql:host=localhost;dbname=baseestadisticas1'
      username: miusuario
      password: ********

In the Action:

$mConexion1Options = Doctrine_Manager::getInstance()->getConnection('conexion1')->getOptions();
preg_match('/dbname=(.*)/', $mConexion1Options['dsn'], $mDbConexion1);

Then, the dbname is:

echo $mDbConexion1[1]; //basegestion1
1

Use this code:

$myConnection           = Doctrine_Manager::getInstance()->getConnection('doctrine')->getOptions();
$dsnInfo                = $this->parseDsn($myConnection['dsn']);
$settings               = array();
$settings['dbUser']     = (string) $myConnection["username"];
$settings['dbPassword'] = (string) $myConnection["password"];
$settings['dbHost']     = (string) $dsnInfo["host"];
$settings['dbName']     = (string) $dsnInfo['dbname'];


private function parseDsn ($dsn)
{
  $dsnArray            = array();
  $dsnArray['phptype'] = substr($dsn, 0, strpos($dsn, ':'));
  preg_match('/dbname  = (\w+)/', $dsn, $dbname);
  $dsnArray['dbname']  = $dbname[1];
  preg_match('/host    = (\w+)/', $dsn, $host);
  $dsnArray['host']    = $host[1];

  return $dsnArray;
}
1

Answer for modern (as of October 2022) Doctrine DBAL 3.4.5 and Persistence 2.5.4

In the modern iteration of Doctrine, I could only find a way to get the name of a Doctrine connection (as configured in the doctrine.yaml file) by injecting ManagerRegistry:

use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;

// ...

    private function getConnectionName(ManagerRegistry $managerRegistry, Connection $targetConnection): string
    {
        foreach ($managerRegistry->getConnections() as $connectionName => $connection) {
            if ($targetConnection === $connection) {
                return $connectionName;
            }
        }

        return '<unrecognized connection>';
    }

// ...
0

Try this one

$conn = Doctrine_Manager::getInstance()->getConnection('doctrine')->getOptions();

$dns_array = split(';', $conn['dsn']);
preg_match('/host=(.*);/', $dns_array, $dbhost);
preg_match('/dbname=(.*)/', $dns_array, $dbname);

$dbname = $dbname;
$dbhost = $dbhost;
$dbuser = $conn['username'];
$dbpass = $conn['password']; 

Updated

As Function split() is deprecated so here is recommended way.

$dns_array = explode(';', $conn['dsn']);
preg_match('/host=(.*)/', $dns_array[0], $dbhost);
preg_match('/dbname=(.*)/', $dns_array[2], $dbname);

$dbhost = $dbhost[1];
$dbname = $dbname[1];
$dbuser = $conn['username'];
$dbpass = $conn['password'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.