Find current Doctrine database connection settings in symfony Find current Doctrine database connection settings in symfony symfony symfony

Find current Doctrine database connection settings in symfony


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


for example:

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


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