5

I have a Symfony2 application that I want to make multi-tenant by the use of one database pr tenant (some don't consider this to be multi-tenancy, but that's not really the point).

The documentation describes how to accomplish this. However, I want to be able to create tenants dynamically, and writing the new database connection details (and entity managers) to the config.yml file directly seems messy. I would rather have a separate database which holds the tenants and their connections, and then select the proper connection/em based on an identifier (fetched, for instance, from a the subdomain of the app - clientname.app.com).

Using this approach I should be able to accomplish this, but will at the same time probably break the ability to specify the database connection and/or entity manager when running the command line commands for updating database schemas and the likes.

Provided that what I want to do make sense, is there a clever way to achieve this?

4

4 に答える 4

2

ユーザーの資格情報に基づいてカスタム エンティティ マネージャーを生成するサービスを作成します。

$this->get('my.db.service')->getEmForUser('bob');

次に、あなたのサービスは次のようになります

class EntityManagerService
{

   function __construct($doctrine)
   { ... }

   function getEmForUser($user)
   {
      //look up Bob's connection details in your connection db
      //and get them using the globally configured entity manager

      //create Entity Manager using bob's config

      return $em.

    }

これは物事を行うための最も再利用可能な方法であり、Symfony2 が使用する依存性注入パターンに適合します。

このクラスのインスタンスを返す必要があります

https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/EntityManager.php

于 2013-04-02T14:27:38.563 に答える
0

私があなたの質問の範囲を把握したかどうかはわかりませんが、これを使用して別のデータベースに接続します:

    $connectionFactory = $this->container->get('doctrine.dbal.connection_factory');
    $conn = $connectionFactory->createConnection(array(
        'driver' => 'pdo_mysql',
        'user' => 'mattias',
        'password' => 'nkjhnjknj',
        'host' => 'fs1.uuyh.se',
        'dbname' => 'csmedia',
    ));
    return $conn;
于 2013-05-09T14:49:34.397 に答える