symfony2动态数据库连接使用原则 [英] symfony2 dynamic database connection using doctrine

查看:120
本文介绍了symfony2动态数据库连接使用原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在Symfony 2中使用了多个数据库连接,但没有这样做。它是通过config.yml文件或动态数据库完成的,其中所有数据库具有相同的模式/实体。



但对于我的情况,数据库是基于子域确定的,数据库模式对于所有子域都不相同。



例如:

test1.example.com =>应该加载test1 db

test2.example.com =>将加载test2 db



在数据库级别创建的test1和test2数据库不同,在原则中没有实体条目。



任何人都可以帮助我在Symfony 2中做到这一点。

解决方案

在我看来,使用教义ODM不是正确的方法。您仍然可以使用Doctrine连接数据库并进行查询。但是,如果没有实体类,实体管理器的使用似乎是不合适的。



使用Doctrine进行连接处理



以下是使用原则创建与数据库的连接连接 class:

  / ** @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 ))
);

现在您可以使用 $ connection 作为简单的 PDO 对象:

  $ connection-> executeQuery * FROM your_table'); 

您可以将此代码添加为服务,以使其随处可见。

如果要连接到不同域的其他数据库,您可以使用此代码来识别域:

  $这 - > Request()方法 - >和getHost(); 

要访问操作中的域,请执行以下操作:

  public function yourAction(Request $ request,/ * ... * /)
{
// Controller extends集装箱所以需要在这里:
$ connectionFactory = $ this-> get('doctrine.dbal.connection_factory');

//也可以访问域名:
$ domain = $ request-> getHost();
}


I am trying to have multiple database connection in Symfony 2 with doctrine but not able to do so.

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 .

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
test2.example.com => Will load test2 db

Both test1 and test2 DB are different are created at DB level and not having entity entries in doctrine.

Can anyone please help me how to do this in Symfony 2.

解决方案

It seems to me that using Doctrines ODM is not the right way to approach this. You can still use Doctrine to connect to databases and query them. But if you have no entity classes the use of an entity manager seems to be inappropriate.

Use Doctrine for Connection handling

Here is how you create a connection to a Database with the doctrine Connection class:

/** @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->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();
}

这篇关于symfony2动态数据库连接使用原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆