Answer the question
In order to leave comments, you need to log in
How to dynamically change the database in symfony 3?
Hello.
The situation is as follows: there is an unlimited number of databases of the same type (the same in structure but different in content), which database should be used at one time or another is determined during the execution of the script. Accordingly, connect to it and use it by default.
How to do it?
Answer the question
In order to leave comments, you need to log in
I do not know the whole task, but the option of resolving through the environment may be suitable.
In app.php we get the required database and connect the appropriate environment, in the config of which access to the database is connected
// $env = ...;
$kernel = new AppKernel($env, false);
$kernel->loadClassCache();
So, I'll write what I've been able to do so far.
1. In the configuration file, we specify the database that we will use during development, and it will also contain the final structure.
2. After the database we need becomes known, we connect as follows:
<?
//получаем и закрываем соединение
$connection = $this->get('doctrine.dbal.default_connection');
$connection->close();
//Дальше варварство
$ReflectionObject = new \ReflectionObject($connection);
$Params = $refConn->getProperty('_params');
$Params->setAccessible(true);
$p = $refParams->getValue($connection);
$p['dbname'] = ;
$p['user'] = ;
$p['password'] = ;
$Params->setValue($connection, $p);
$Params->setAccessible(false);
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question