Find current Doctrine database connection settings in symfony

rlandster picture rlandster · May 16, 2011 · Viewed 23.7k times · Source

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)?

Answer

Andrew Atkinson picture Andrew Atkinson · Oct 22, 2014

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