Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find current Doctrine database connection settings in symfony

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

like image 905
rlandster Avatar asked May 16 '11 00:05

rlandster


People also ask

How do I know my doctrine version?

Check out the file vendor/doctrine/orm/lib/Doctrine/ORM/Version. php , there is a constant in there that shows the version. It's also accessible from a running app but this is easier.

What is Doctrine in Symfony?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.

What is doctrine schema update?

$ php bin/console doctrine:schema:update --force. Tip. Actually, this command is incredibly powerful. It compares what your database should look like (based on the mapping information of your entities) with how it actually looks, and executes the SQL statements needed to update the database schema to where it should be ...

Is doctrine part of Symfony?

Doctrine is totally decoupled from Symfony and using it is optional. This chapter is all about the Doctrine ORM, which aims to let you map objects to a relational database (such as MySQL, PostgreSQL or Microsoft SQL).


2 Answers

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

like image 101
Andrew Atkinson Avatar answered Oct 05 '22 09:10

Andrew Atkinson


for example:

foreach(Doctrine_Manager::getInstance()->getConnections() as $connection){
  $conn = $connection->getOptions();
  preg_match('/host=(.*);/', $conn['dsn'], $host);
  var_dump($host);
}
like image 7
Flask Avatar answered Oct 05 '22 10:10

Flask