2016-01-16 43 views
2

我的網站的每個連接成員都有其數據庫。 這裏是 「USER_1」 學說配置:zf2 +爲每個成員指定一個不同的數據庫

return array(
'doctrine' => array(
    'connection' => array(
     'orm_default' => array(
      'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver', 
      'params' => array(
       'host'  => 'localhost', 
       'port'  => '3306', 
       'user'  => 'user_1', 
       'password' => 'psswd_user_1', 
       'dbname' => 'database_user_1', 
       'charset' => 'utf8', 
       'driverOptions' => array (1002 => 'SET NAMES utf8'), 
      )),),),); 

有沒有辦法來取代: 'USER_1', 'psswd_user_1' 和 'database_user_1' 與 'user_x', 'psswd_user_x' 和「database_user_x 'for user_x?

我不知道該怎麼做! 我想避免複製相同的代碼爲每個用戶...

謝謝大家幫忙

回答

0

正確的方式做,這可能注入,您需要創建連接時的配置。我無法找到任何可以執行此操作的事件,因此您必須找到正確的服務管理器密鑰來覆蓋。

源代碼挖一點點,我發現,這些選項被髮送到DoctrineORMModule\Options\DBALConnection實例,這個實例由DoctrineORMModule\Service\DBALConnectionFactory

創建你需要像這樣的東西來覆蓋這家工廠:

<?php 
namespace MyModule\Service; 
use DoctrineORMModule\Service\DBALConnectionFactory; 
use Zend\ServiceManager\ServiceLocatorInterface; 

class MyDBALConnectionFactory extends DBALConnectionFactory 
{ 

    public function getOptions(ServiceLocatorInterface $sl, $key, $name = null) 
    { 
     $options = parent::getOptions($sl, $key, $name); 

     // override for everyone that needs a DBALConnection 
     if ($this->getOptionClass() === 'DoctrineORMModule\Options\DBALConnection') { 

      // set custom parameters here 
      // maybe fetch the current user with $sl->get('...') 
      $params = [/* ... */]; 
      $options->setParams($params); 
     } 

     return $options; 
    } 
} 

然後你只需告訴服務管理器看:

<?php 
return [ 
    ... 
    'doctrine' => [ 
     'doctrine_factories' => [ 
      'connection' => 'MyModule\Service\DBALConnectionFactory', 
     ] 
    ] 
    ... 
]; 
+0

的Bonjour等的MerciClémentJe vais travailler sur cette滑雪道 – user2670354

+0

樂意提供幫助。讓我知道,如果此代碼不能按預期工作,我們可能不會有相同版本的ZF2和Doctrine –

+0

Je le ferai biensûravec plaisir – user2670354

0

感謝亞歷杭德羅·塞拉亞。 1st link2d link

我希望它會有用。 我知道這不是完美的,但我不能做得更好!我想批評。

在配置/自動加載/ doctrine.local.php:

'doctrine' => array(
'connection' => array(
    'orm_default' => array(
     'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver', 
     'params' => array(
      'host'  => 'localhost', 
      'port'  => '3306', 
      'user'  => 'root', 
      'password' => '', 
      'dbname' => 'gestion_toto_default', 
      'charset' => 'utf8', 
      'driverOptions' => array(1002 => 'SET NAMES utf8'), 
     ) 
    ), 
    'orm_toto_users' => array(
     'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver', 
     'params' => array(
      'host'  => 'localhost', 
      'port'  => '3306', 
      'user'  => 'root', 
      'password' => '', 
       //gestion_toto_users has 2 tables : users and db_users 
      'dbname' => 'gestion_toto_users', 
      'driverOptions' => array(1002 => 'SET NAMES utf8'), 
     ) 
    ), 
    'dynamic_orm' => array(
     'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver', 
     'params' => array(
      'host'  => 'localhost', 
      'port'  => '3306', 
      'user'  => '', 
      'password' => '', 
      'dbname' => '', 
      'driverOptions' => array(1002 => 'SET NAMES utf8'), 
     ), 
    ), 
), 

'driver' => array(
    'orm_toto_users' => array(
     'class' => 'Doctrine\ORM\Mapping\Driver\DriverChain', 
     'drivers' => array(
      __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' 
     ) 
    ), 
    'dynamic_orm' => array(
     'drivers' => array(
      __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' 
     ) 
    ), 
), 

'entitymanager' => array(
    'orm_toto_users' => array(
     'connection' => 'orm_toto_users', 
     'configuration' => 'orm_default' 
    ), 
    'dynamic_orm' => array(
     'connection' => 'dynamic_orm', 
    ), 
), 

'eventmanager' => array(
    'orm_toto_users' => array() 
), 

'sql_logger_collector' => array(
    'orm_toto_users' => array(), 
), 

'entity_resolver' => array(
    'orm_toto_users' => array() 
),), 

module.config.php:

'doctrine' => array(
    'driver' => array(
     __NAMESPACE__ . '_driver' => array(
      'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 
      'cache' => 'array', 
      'paths' => array(
       __DIR__ . '/../src/' . __NAMESPACE__ . '/Entity' 
      ), 
     ), 
     'orm_default' => array(
      'drivers' => array(
       __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' 
      ) 
     ), 
    ), 
    'authentication' => array(
     'orm_default' => array(
      'object_manager' => 'doctrine.entitymanager.orm_toto_users', 
      'identity_class' => 'MyModule\Entity\User', 
      'identity_property' => 'identifiant', 
      'credential_property' => 'password', 
      'credential_callable' => function(\MyModule\Entity\User $user, $passwordGiven) { 
       $bcrypt = new \Zend\Crypt\Password\Bcrypt(); 
       return $bcrypt->verify($psswdGiven, $user->getPsswd()) && $user->getIsactif(); 
      }, 
     ), 
    ), 
), 
在Xcontroller

public function getEntityManager() 
{ 
    if (null === $this->em) { 

     $this->em = $this->getServiceLocator()->get('dynamic_entity_manager'); 
    } 
    return $this->em; 
} 
在UserController中

public function getEntityManager() 
{ 
    if (null === $this->em) { 
     $this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_toto_users'); 
    } 
    return $this->em; 
} 

在DynamicEMFactory.php:

class DynamicEMFactory implements FactoryInterface { 
public function createService(ServiceLocatorInterface $serviceLocator) 
{ 
     // Get current user 
    $authService = $serviceLocator->get('Zend\Authentication\AuthenticationService'); 
    if (! $authService->hasIdentity()) { 
     throw new \RuntimeException(
      'It is not possible to create a dynamic entity manager before a user has logged in' 
     ); 
    } 

    $user = $authService->getIdentity(); 
    $db_User = $user->getUser_db()->getDbuser(); 
    $db_Psswd = $user->getUser_db()->getDbpsswd(); 
    $db_Name = $user->getUser_db()->getDbname(); 

     // Update connection config 
    $globalConfig = $serviceLocator->get('config'); 
    $globalConfig['doctrine']['connection']['dynamic_orm']['params']['user'] = $db_User; 
    $globalConfig['doctrine']['connection']['dynamic_orm']['params']['password'] = $db_Psswd; 
    $globalConfig['doctrine']['connection']['dynamic_orm']['params']['dbname'] = $db_Name; 

    $isAllowOverride = $serviceLocator->getAllowOverride(); 
    $serviceLocator->setAllowOverride(true); 
    $serviceLocator->setService('config', $globalConfig); 
    $serviceLocator->setAllowOverride($isAllowOverride); 

    return $serviceLocator->get('doctrine.entitymanager.dynamic_orm'); 
} 

}

在module.config

'service_manager' => array(
    'factories' => array(

     'dynamic_entity_manager' => 'XXX\Service\Factory\DynamicEMFactory', 

在onBootstrap(改變appearence):

$authService = $serviceManager->get('Zend\Authentication\AuthenticationService'); 
    if ($authService->getIdentity()) { 
     $em = $serviceManager->get('dynamic_entity_manager'); 
    } else { 
     $em = $serviceManager->get('doctrine.entitymanager.orm_default'); 
    } 
    $viewModel = $e->getApplication()->getMvcEvent()->getViewModel(); 
    $query = $serviceManager->get('param_user'); 
    $tab = $query->getReponse($em); 
    $nom_theme = $tab['something'])); 
    $viewModel->nom_theme = $nom_theme;//to layout 
相關問題