V
V
Vitaly Sorokin2017-04-02 15:06:44
symfony
Vitaly Sorokin, 2017-04-02 15:06:44

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

4 answer(s)
B
BoShurik, 2017-04-03
@SorokinWS

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();

Upd :
Based on your comment above
The same script uses several clients, which database is used - is determined through the subdomain. The list of subdomains and their databases is stored in the service database.
I think this option will do:
In app.php, make a request to the main database through the usual \PDO. Pulling connection parameters from there, add them to the environment via putenv , and use these variables in the config

A
Alexey Skobkin, 2017-04-02
@skobkin

symfony.com/doc/current/doctrine/multiple_entity_m...

V
Vitaly Sorokin, 2017-04-02
@SorokinWS

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

D
dimarick, 2017-04-06
@dimarick

Make your own entity manager for each base, and choose from them in KernelRequest and save them in default_entity_manager and default_connection. Unless there are very many such bases

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question