2

我网站的每个连接成员都有自己的数据库。这是“user_1”的教义配置:

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_x'、'psswd_user_x' 和 'database_user_x' 替换 user_x 的 'user_1'、'psswd_user_1' 和 'database_user_1'?

我不知道该怎么做!我想避免为每个用户复制相同的代码......

谢谢你的帮助

4

2 回答 2

1

执行此操作的正确方法可能是在创建连接时注入您需要的配置。我找不到任何可以挂钩的事件,因此您必须找到正确的服务管理器密钥来覆盖。

通过一点源代码挖掘,我发现这些选项被发送到一个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',
        ]
    ]
    ...
];
于 2016-01-18T23:55:42.547 回答
0

感谢亚历杭德罗·塞拉亚。 第一个链接第二个链接

我希望它会有用。我知道这并不完美,但我不能做得更好!我想批评。

在 config/autoload/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()
),),

模块.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;
}

在用户控制器中:

    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 (改变外观):

    $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
于 2016-01-23T19:24:37.283 回答