7

我有一个核心配置数据库,每一行都是一个带有一些基本配置等的“应用程序”。
一旦你选择了你的应用程序,我想使用该行的属性(ID)连接到数据库,主机也可能会改变基于行。

我想要的是注册一个使用这些详细信息设置 Doctrine 服务的服务,如果你在网站上需要它的地方(我知道基于 URI)。

我正在使用实体管理器和各种 Doctrine Listeners/Event subs

我玩过 ConnectionFactory,但这似乎会导致订阅者出现问题。

连接可以透明地修改 Doctrine 服务的东西的最佳方法是什么,以便控制器可以在不知道它们连接到哪个数据库主机和数据库名称的情况下采取行动?

这种类型的每个数据库都将具有相同的结构,因此所有实体映射都是正确的。

我正在寻找一个非常干净的实现,希望使用服务容器来避免任何“黑客攻击”。

有没有人知道这样做的?

4

5 回答 5

11

结合起来,这两个帖子帮助我解决了我自己非常相似的问题。这是我的解决方案,也许对其他人有用:

<?php

namespace Calitarus\CollaborationBundle\EventListener;

use Symfony\Component\HttpFoundation\Request;
use Doctrine\DBAL\Connection;
use Exception;
use Monolog\Logger;



class DatabaseSwitcherEventListener {

    private $request;
    private $connection;
    private $logger;

    public function __construct(Request $request, Connection $connection, Logger $logger) {
        $this->request = $request;
        $this->connection = $connection;
        $this->logger = $logger;
    }


    public function onKernelRequest() {
        if ($this->request->attributes->has('_site')) {
            $site = $this->request->attributes->get('_site');

            $connection = $this->connection;
            $params     = $this->connection->getParams();

            $db_name = 'br_'.$this->request->attributes->get('_site');
            // TODO: validate that this site exists
            if ($db_name != $params['dbname']) {
                $this->logger->debug('switching connection from '.$params['dbname'].' to '.$db_name);
                $params['dbname'] = $db_name;
                if ($connection->isConnected()) {
                    $connection->close();
                }
                $connection->__construct(
                    $params, $connection->getDriver(), $connection->getConfiguration(),
                    $connection->getEventManager()
                );

                try {
                    $connection->connect();
                } catch (Exception $e) {
                    // log and handle exception
                }
            }
        }
    }
}

为了让它工作,我设置 services.yml 如下:

services:
    cc.database_switcher:
        class:      Calitarus\CollaborationBundle\EventListener\DatabaseSwitcherEventListener
        arguments:  [@request, @doctrine.dbal.default_connection, @logger]
        scope:      request
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

我有这个路由配置来获取 _site 参数,在我的情况下,它是 URL 的一部分,但您可能可以根据您的设置以其他方式获取它:

resource: "@CCollabBundle/Controller"
type:     annotation
prefix:   /{_site}
defaults:
 _site: default
于 2014-07-05T10:09:04.493 回答
10

这是新的和改进的非反射版本

#services.yml
acme_app.dynamic_connection:
    class: %acme.dynamic_doctrine_connection.class%
    calls:
        - [setDoctrineConnection, [@doctrine.dbal.default_connection]]


<?php

namespace Acme\Bundle\AppBundle;

use Doctrine\DBAL\Connection;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Exception;

class DynamicDoctrineConnection
{
    /**
     * @var Connection
     */
    private $connection;

    /**
     * Sets the DB Name prefix to use when selecting the database to connect to
     *
     * @param  Connection       $connection
     * @return SiteDbConnection $this
     */
    public function setDoctrineConnection(Connection $connection)
    {
        $this->connection = $connection;

        return $this;
    }

    public function setUpAppConnection()
    {
        if ($this->request->attributes->has('appId')) {
            $connection = $this->connection;
            $params     = $this->connection->getParams();

            // we also check if the current connection needs to be closed based on various things
            // have left that part in for information here
            // $appId changed from that in the connection?
            // if ($connection->isConnected()) {
            //     $connection->close();
            // }

            // Set default DB connection using appId
            //$params['host']   = $someHost;
            $params['dbname'] = 'Acme_App'.$this->request->attributes->get('appId');

            // Set up the parameters for the parent
            $connection->__construct(
                $params, $connection->getDriver(), $connection->getConfiguration(),
                $connection->getEventManager()
            );

            try {
                $connection->connect();
            } catch (Exception $e) {
                // log and handle exception
            }
        }

        return $this;
    }
}
于 2013-02-27T11:23:19.703 回答
5

在 symfony 4 中,你可以用一个包装类来完成它:

# doctrine.yaml
doctrine:
    dbal:
      connections:
        default:
          wrapper_class: App\Service\Database\DynamicConnection

该类只是扩展了原始连接:

class DynamicConnection extends \Doctrine\DBAL\Connection
{

    public function changeDatabase(string $dbName)
    {
        $params = $this->getParams();

        if ($this->isConnected())
            $this->close();

        if (isset($params['url'])) {
            $params['url'] = preg_replace(
                sprintf("/(?<=\/)%s/", preg_quote($this->getDatabase())),
                $dbName,
                $params['url']
            );
        }

        if (isset($params['dbname']))
            $params['dbname'] = $dbName;

        parent::__construct(
            $params,
            $this->_driver,
            $this->_config,
            $this->_eventManager
        );

    }
}
于 2018-02-02T00:27:33.817 回答
4

Symfony 4

在服务定义中使用装饰器模式的最简洁方式:

首先创建一个像 App\Factory\Authentication\DatabaseConnectionFactory 这样的自定义类,然后这个类将实例化为一个 dictionary.dbal.connection_factory 的实例。

#services.xml
App\Factory\Authentication\DatabaseConnectionFactory:
        decorates: doctrine.dbal.connection_factory
        arguments:
            $wrappedConnectionFactory: '@App\Factory\Authentication\DatabaseConnectionFactory.inner'

在我们的自定义连接工厂类中模拟 createConnection() 函数并通过调用 WrappedConnectionFactory 上的函数来执行原始的 createConnection() 逻辑(= 学说.dbal.connection_factory)。

/** App\Factory\Authentication\DatabaseConnectionFactory
 * @param array              $params
 * @param Configuration|null $config
 * @param EventManager|null  $eventManager
 * @param array              $mappingTypes
 *
 * @throws \DomainException
 *
 * @return mixed
 */
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = [])
{
    $params['url'] = $this->databaseConnectionUrlService->getDatabaseConnectionUrlForApiUser($this->apiUser, $params['url'] );

    return $this->wrappedConnectionFactory->createConnection($params, $config, $eventManager, $mappingTypes);
}
于 2019-04-09T12:01:35.957 回答
0

我查看了您的服务并尝试实现它,但看起来您缺少一些需要传递给构造函数的参数。这是一个应该可以工作的更新版本:

#services.yml
parameters:
    acme_page.dynamic_doctrine_connection.class: Acme\Bundle\PageBundle\DynamicDoctrineConnection

services:
    acme_page.dynamic_doctrine_connection:
        class:      %acme_page.dynamic_doctrine_connection.class%
        arguments:  [@request, @doctrine.dbal.client_connection, @doctrine]
        scope:      request
        calls:
            - [setContainer, [@service_container]]
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

//DynamicDoctrineConnection.php
<?php

namespace Acme\Bundle\PageBundle;

use Symfony\Component\HttpFoundation\Request;
use Doctrine\DBAL\Connection;
use Doctrine\Bundle\DoctrineBundle\Registry;

/**
 * Creates a Doctrine connection from attributes in the Request
 */
class DynamicDoctrineConnection
{
    private $request;
    private $defaultConnection;
    private $doctrine;

    public function __construct(Request $request, Connection $defaultConnection, Registry $doctrine)
    {
        $this->request           = $request;
        $this->defaultConnection = $defaultConnection;
        $this->doctrine          = $doctrine;
    }

    public function onKernelRequest()
    {
        if ($this->request->attributes->has('appId')) {

            $dbName             = 'Acme_App_'.$this->request->attributes->get('appId');

            $this->defaultConnection->close();

            $reflectionConn     = new \ReflectionObject($this->defaultConnection);
            $reflectionParams   = $reflectionConn->getProperty('_params');
            $reflectionParams->setAccessible(true);

            $params             = $reflectionParams->getValue($this->defaultConnection);
            $params['dbname']   = $dbName;

            $reflectionParams->setValue($this->defaultConnection, $params);
            $reflectionParams->setAccessible(false);

            $this->doctrine->resetEntityManager('default');
    }
}
于 2013-12-28T04:05:47.553 回答