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)?
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.
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.
$ 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 ...
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).
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With