Switch and manage the Doctrine connection at runtime.
composer config repositories.turbolabit/php-doctrine-runtime-manager git https://github.com/TurboLabIt/php-doctrine-runtime-manager.git
composer require turbolabit/php-doctrine-runtime-manager:dev-main
If the application uses a single-DB connection:
# config/packages/doctrine.yaml
doctrine:
dbal:
# ....
wrapper_class: TurboLabIt\DoctrineRuntimeManager\DoctrineRuntimeManager
# ....
If the application uses multiple connections, you must configure the wrapper on each connection:
# config/packages/doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
# ....
wrapper_class: TurboLabIt\DoctrineRuntimeManager\DoctrineRuntimeManager
# ...
wordpress:
# ....
# wrapper_class: TurboLabIt\DoctrineRuntimeManager\DoctrineRuntimeManager
# ...
<?php
namespace App\Service;
class Language
{
protected EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function setCurrent($currentLn)
{
$this->em->getConnection()->selectDatabaseByAppend($currentLn);
}
}
git clone git@github.com:TurboLabIt/php-doctrine-runtime-manager.git
cd php-doctrine-runtime-manager
clear && bash script/test_runner.sh