简体   繁体   English

使用doctrine的symfony2动态数据库连接

[英]symfony2 dynamic database connection using doctrine

I am trying to have multiple database connection in Symfony 2 with doctrine but not able to do so. 我试图在Symfony 2中使用doctrine进行多个数据库连接但不能这样做。

I have searched extensively in Google and stack-overflow but everywhere it's done via config.yml file or dynamic database where all DB's have same schema/entities . 我已经在谷歌和堆栈溢出中进行了广泛搜索,但无论在哪里,它都是通过config.yml文件或动态数据库完成的,其中所有数据库都具有相同的模式/实体。

But for my case the database is determined based on subdomain and the database schema is not same for all subdomains. 但对于我的情况,数据库是基于子域确定的,并且所有子域的数据库架构都不相同。

Ex: 例如:
test1.example.com => Should load test1 db test1.example.com =>应加载test1 db
test2.example.com => Will load test2 db test2.example.com =>将加载test2 db

Both test1 and test2 DB are different are created at DB level and not having entity entries in doctrine. test1和test2 DB都是不同的,它们是在DB级别创建的,并且在doctrine中没有实体条目。

Can anyone please help me how to do this in Symfony 2. 任何人都可以帮助我在Symfony 2中如何做到这一点。

It seems to me that using Doctrines ODM is not the right way to approach this. 在我看来,使用Doctrines ODM并不是解决这个问题的正确方法。 You can still use Doctrine to connect to databases and query them. 您仍然可以使用Doctrine连接到数据库并查询它们。 But if you have no entity classes the use of an entity manager seems to be inappropriate. 但是如果你没有实体类,那么使用实体管理器似乎是不合适的。

Use Doctrine for Connection handling 使用Doctrine进行连接处理

Here is how you create a connection to a Database with the doctrine Connection class: 以下是使用doctrine Connection类创建与数据库的Connection

/** @var \Doctrine\Bundle\DoctrineBundle\ConnectionFactory $connectionFactory */
$connectionFactory = $this->getContainer()->get('doctrine.dbal.connection_factory');
$connection = $connectionFactory->createConnection(
    array('pdo' => new \PDO("mysql:host=$hostname;dbname=$dbname", $username, $password))
);

Now you can use $connection as a simple PDO object: 现在您可以将$connection用作简单的PDO对象:

$connection->executeQuery('SELECT * FROM your_table');

You could add this code as a service to make it accessible everywhere. 您可以将此代码添加为服务,以使其随处可访问。
If you want to connect to a different database for a different domain you can use this code to identify the domain: 如果要连接到其他域的其他数据库,可以使用此代码来标识域:

$this->getRequest()->getHost();

To access the domain in an action do this: 要在操作中访问域,请执行以下操作:

public function yourAction(Request $request, /* ... */)
{
    // the Controller extends the Container. So need to get it here:
    $connectionFactory = $this->get('doctrine.dbal.connection_factory');

    // also access the domain like this:
    $domain = $request->getHost();
}

Thanks to byf-ferdy ( https://stackoverflow.com/a/20444097/2976700 ) , i am able to figure out how to use other DB having no doctrine entities. 感谢byf-ferdy( https://stackoverflow.com/a/20444097/2976700 ),我能够弄清楚如何使用没有教义实体的其他数据库。 Just use the following code in your action controller 只需在动作控制器中使用以下代码即可

$connectionFactory = $this->get('doctrine.dbal.connection_factory');                
$connection = $connectionFactory->createConnection(
                array('pdo' => new \PDO("mysql:host=$hostname;dbname=$dbname", 
                       $username,$password))
 );
 $query = $connection->executeQuery('SELECT * FROM multi_client');
 $results = $query->fetchAll();

To know the subdomain accessed one can use $domain = $request->getHost(); 要知道访问的子域名,可以使用$ domain = $ request-> getHost();

Accordingly one change change the DB name and other parameters. 因此,一个更改会更改数据库名称和其他参数。 Hope it helps others 希望它能帮助别人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM