bksunday
bksunday

Reputation: 745

How to get password used for a doctrine dbal connection in symfony2?

I would like to retrieve the password currently used by a doctrine2 dbal connection, in symfony2.

If I use $kernel->getContainer()->getParameter('database_password'), I can find the one defined in parameters.yml but if I define different password for some environments (in config_test.yml for example), the data from parameters.yml is useless.

Now if I look thru $kernel->getContainer()->getParameterBag()->all(), I still cannot find it.

And in case you wonder why? I am using a lock mechanism in a function, and I am trying to use register_shutdown_function() to remove that lock in case there is a fatal error, using php built-in mysql or pdo function since at that point, most of the framework is down.

Upvotes: 1

Views: 1480

Answers (2)

HMagdy
HMagdy

Reputation: 3295

if you need to access Container and Connection via kernel object use below code:

$connection = $kernel->getContainer()->get('doctrine')->getConnection();

and if you need to get and set Container Parameters you can use below code:

$container->setParameter('database_name', 'xyz');
$db_password = $container->getParameter("database_password");

Upvotes: 2

Gintro
Gintro

Reputation: 635

If you cannot retrieve it, then you will not have a DB connection either.

Parameters from test environment won't be loaded in the dev, prod, ... environment! In case of overriding, define all your connection parameters in parameters.yml

//parameters.yml
database_prod_name: prod
database_prod_user: prod_user
database_prod_password: prod_password

database_dev_name: dev
database_dev_user: dev_user
.....

//config_prod.yml
doctrine:
    dbal:
        dbname:   "%database_prod_name%"
        user:     "%database_prod_user%"
        password: "%database_prod_password%"
....

Same goes for all other environments your using.

Upvotes: 1

Related Questions