Advertisement
Guest User

Dynamic DB connection Symfony

a guest
Mar 4th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Services\DbService;
  4.  
  5. use Doctrine\ORM\EntityManager;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Doctrine\DBAL\Connection;
  8. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  9. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  10.  
  11. class DbFactory
  12. {
  13.    
  14.     protected $dynamicEm;  //Entity manager for dynamically connected database
  15.    
  16.     // $dbConnection is the connection from your config, set in the config with blank values.
  17.     // $em is the related entity manager.
  18.     public function __construct(AuthorizationCheckerInterface $authChecker, Connection $dbConnection, EntityManager $em, TokenStorageInterface $tokenStorage)
  19.     {
  20.         if(!$authChecker->isGranted("IS_AUTHENTICATED_FULLY")) {
  21.             throw new AccessDeniedException();
  22.         }
  23.  
  24.         $connectionDetails = $tokenStorage->getToken()->getUser()->getStuffForConnection();
  25.        
  26.         $user = $connectionDetails->get("dbUser");
  27.        
  28.         $dbParams = $dbConnection->getParams();
  29.        
  30.         // Check to make sure dynamic connection isn't already setup.
  31.         // Might not actually need to check this, since the factory should only be called once.
  32.         if($dbParams["user"] !== $user) {
  33.            
  34.             $dbParams["user"] = $user;
  35.             $dbParams["host"] = $connectionDetails->getDbHost();
  36.             $dbParams["dbname"] = $connectionDetails->getDbDatabaseName();
  37.             $dbParams["password"] = $connectionDetails->getDbPassword();
  38.  
  39.             if($connectionDetails->isConnected()) {
  40.                 $connectionDetails->close();
  41.             }
  42.             $connectionDetails->__construct(
  43.                     $dbParams, $connectionDetails->getDriver(), $connectionDetails->getConfiguration(), $connectionDetails->getEventManager()
  44.             );
  45.             try {
  46.                 $connectionDetails->connect();
  47.             }
  48.             catch(Exception $ex) {
  49.                 error_log("Failed to connect to DB");
  50.                 throw new Exception("Failed to connect to DB");
  51.                 // Handle this error however you want.
  52.             }
  53.         }        
  54.         $this->dynamicEm = $em;
  55.     }
  56.    
  57.     public function getSkdbEm()
  58.     {
  59.         return $this->dynamicEm;
  60.     }
  61.    
  62.     public function getRepository($repName)
  63.     {
  64.         return $this->dynamicEm->getRepository($repName);
  65.     }
  66.    
  67.     public function getSpecificRepository()
  68.     {
  69.         return $this->dynamicEm->getRepository('Bundle:specific');
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement