Skip to content

TurboLabIt/php-doctrine-runtime-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-doctrine-runtime-manager

Switch and manage the Doctrine connection at runtime.

πŸ“¦ 1. Install it with composer

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

βš™οΈ 2. Mandatory Symfony configuration

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
      # ...

πŸ” 3. Symfony usage

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

πŸ§ͺ Test it

git clone git@github.com:TurboLabIt/php-doctrine-runtime-manager.git
cd php-doctrine-runtime-manager
clear && bash script/test_runner.sh

πŸ”— Sources

About

Switch and manage the Doctrine connection at runtime

Resources

Stars

Watchers

Forks