原则2无限连接数



目前我正在使用Doctrine 2开发ZF2应用程序。应用程序处理多个用户,每个用户都可以通过自己的子域访问应用程序,例如:

user1.example.com
user2.example.com
...
user10.example.com

另外,每个用户都有自己的数据库,对应于他的子域,比如:

db_user1
db_user2
..
db_user10

配置是相当琐碎的有一个或两个学说连接:

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            'orm_default' => array(
                'driverClass' => 'DoctrineDBALDriverPDOMySqlDriver',
                'params' => array(
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'user',
                    'password' => 'pass',
                    'dbname'   => 'database',
                )
            ),
           // Here we can define several configuration alternatives, 
           //use different keys for each one
            'orm_alternative'=> array(
                'driverClass' => 'DoctrineDBALDriverPDOMySqlDriver',
                'params' => array(
                    'host'     => 'localhost',
                    'port'     => '3306',
                    'user'     => 'db_user1',
                    'password' => 'db_pass1',
                    'dbname'   => 'db_user1',
                )
            ),
        ),
     ) 
  );

显然,在我的情况下,上述配置是无用的,因为我有无限数量的连接到不同的数据库。

所以,我的问题是,什么是最好的方法来设置原则工作与无限数量的连接,基于子域。

欢呼,

Vasil Dakov

配置加载顺序如下

1.application.config.php
2.$module->getConfig()
3.$module->get{,*}Config() (or ServiceListeners)
4./config/autoload/{,.*}{global,local}.php

所以你可以在getConfig()功能中添加Module.php中的每个用户配置。

public function getConfig()
{
    return array(
        'user1' => array(
            'driverClass' => 'DoctrineDBALDriverPDOMySqlDriver',
            'params' => array(
                'host'     => 'localhost',
                'port'     => '3306',
                'user'     => 'db_user1',
                'password' => 'db_pass1',
                'dbname'   => 'db_user1',
            )
        )
    );
    //of course you have access to service locator here and could generate your dbconfigs
}

对于我自己的问题,我认为最好的解决方案是通过服务工厂获得EntityManager实例,如下所示:

<?php
namespace ApplicationDoctrineFactory;
use ZendServiceManagerFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;
use DoctrineORMToolsSetup;
use DoctrineORMEntityManager;
use DoctrineORMMappingDriverAnnotationDriver;
use DoctrineCommonAnnotationsAnnotationReader;
use DoctrineCommonAnnotationsAnnotationRegistry;
class EntityManagerServiceFactory implements FactoryInterface {
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST']));
        $paths = array(ROOT_PATH.'/module/Application/src/Application/Entity');
        $account => $this->getAccountTable()->findOneBy(array("subdomain" => $subdomain));
        $isDevMode = true;
        $dbParams = array(
            'driver'   => 'pdo_mysql',
            'user'     => $account['user'],
            'password' => $account['password'],
            'dbname'   => $account['dbname'],
        );
        $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
        $config = Setup::createConfiguration($isDevMode);
        $driver = new AnnotationDriver(new AnnotationReader(), $paths);
        AnnotationRegistry::registerLoader('class_exists');
        $config->setMetadataDriverImpl($driver);
        $entityManager = EntityManager::create($dbParams, $config);
        return $entityManager;
    }
}

效果很好,希望对别人有所帮助。

最新更新