0

I need to change the db name for particular login in (parameter . ini file) in symfony 2.0. So i tried in session .But it is not working .Is it possible to put session value in parameter . ini file ?

My code: (parameter . ini File)

<?php
session_start();
$dbnamenew='';
if($_SESSION['test_db']!=""){
$dbnamenew=$_SESSION['test_db'];
}
else {
$dbnamenew ='test';
}
?>
; These parameters can be imported into other config files
; by enclosing the key with % (like %database_user%)
; Comments start with ';', as in php.ini
[parameters]
    database_driver="pdo_mysql"
    database_host="localhost"
    database_port="22"
    database_name="<?php echo dbnamenew;?>"
    database_user="user"
    database_password="pass"
    mailer_transport="smtp"
    mailer_host="localhost"
    mailer_user=""
    mailer_password=""
    locale="en"
    secret="b538e3680321a85b2e39a3d1772e0b711ff9371c"
j0k
  • 22,600
  • 28
  • 79
  • 90
Niju
  • 29
  • 2
  • 8
  • Parameter.ini isn't parsed by the PHP parser, so that code won't work . I can't actually think of a way to achieve this, thus why this is a comment rather than an answer. – Sean Feb 28 '13 at 12:18
  • Can i pass a value to this file using php or put if conditions in this file in any other method – Niju Feb 28 '13 at 12:37
  • Shall i change the parameter . ini file to parameter.yml file and change config.yml as imports: - { resource: parameters.yml } - { resource: security.yml } .I changed but the white page is displaying. – Niju Feb 28 '13 at 12:57

1 Answers1

0

Even if you could use PHP in the configuration file it wouldn't work since the parsed configuration is cached (so it's only executed once).

If you have a finite number of databases, define a database connection for each one. You can later choose it based on the variable taken from the session.

If you use doctrine's entity manager as well, or if you have infinite number of databases, you'll have to alter the connection parameters on the fly. You can do it with an event listener.

Related answer describing how to change the database connection: Symfony 2 : multiple and dynamic database connection

Community
  • 1
  • 1
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125