与Symfony / Doctrine临时连接到外部数据库 [英] Temporary Connection to External Database with Symfony/Doctrine

查看:193
本文介绍了与Symfony / Doctrine临时连接到外部数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:



我想能够对外部数据库运行查询,以便在用户登录时获取一些所需的数据。我不希望此网站使用外部数据库执行任何任何其他操作。长期来看,它可能需要能够推回数据,但具体来说,我不希望symfony尝试构建外部数据库的架构,它应该只是让它独自一人,并允许不时的连接。 / p>

详细信息:



我正在尝试创建一个到另一个symfony应用程序的数据库的临时连接,我可以我似乎找出了如何做到这一点。



我有一个现有的symfony站点被设置并运行。我正在为这个主要网站的用户创建一种管理系统。管理系统将为每个选择加入的用户分配部署,因此它也将拥有与之相关联的数据库。但是,这个管理系统需要从主站点系统访问2或3个表。



我已经尝试在管理系统中的databases.yml中添加单独的条目来创建连接到这两个数据库,但是每当我构建所有数据库时,它都希望将我的架构放在两个数据库中。



我提出的最好的想法是将连接:管理我所有的表都用于管理系统,并且还连接了main_site在所有表中的主站点。但是,这需要我在管理系统和主站点上维护yml文件,以确保他们保持最新状态。



我希望是感谢您的帮助:D

解决方案

对于无法在远程站点使用DQL的临时连接,只需通过 Doctrine_Manager 打开连接:

  $ conn = Doctrine_Manager :: getInstance() - > openConnection('mysql:// username:password @ localhost / database','connection 2',false) 

确保第三个参数为false,使其不成为当前连接。然后,您可以使用PDO在连接上运行查询:

  $ conn-> exec('SELECT * FROM table WHERE conditionalon = ?',array('value')); 

如果您想要使用DQL和远程架构,则必须执行此操作您概述的方式:定义两个连接并在两个地方维护模式(您的版本控制应该能够处理这一点)。


Summary:

I want to be able to run a query against an external database to grab some needed data at the time the user logs in. I don't want this site to do anything else with the external database. Long term, it may need to be able to push data back, but specifically, I don't want symfony to try to build the schema for the external db, it should just leave it alone and allow a connection from time to time.

Details:

I'm trying to create a temporary connection to a database of another symfony application and I can't seem to figure out how to do it.

I have an existing symfony site that is set up and running. I'm trying to create a sort of management system for users of this main site. The management system will have separate deployments for each users that opts in to it, so it will also have its own database associated with it. However, this management systems needs access to 2 or 3 tables from the main sites system.

I've tried adding separate entries in databases.yml in the management system to create connections to both databases, however whenever I build all, it wants to put my schema in both databases.

The best idea I have come up with is placing connection: management in all my tables for the management system, and also place connection: main_site in all the tables from the main site. However, that would require me to maintain the yml files both in the management system and on the main site to make sure they stayed current with each other.

I hope that was even slightly clear.

Thanks for the help :D

解决方案

For a temporary connection without the ability to use DQL on the remote site, just open the connection via Doctrine_Manager:

$conn = Doctrine_Manager::getInstance()->openConnection('mysql://username:password@localhost/database', 'connection 2', false);

Make sure the 3rd argument is false so that it doesn't become the current connection. You can then run queries on the connection using PDO:

$conn->exec('SELECT * FROM table WHERE conditon = ?', array('value'));

If you want to be able to use DQL and the remote schema, you'll have to do it the way you outlined: define two connections and maintain the schema in both places (your version control should be able to handle this).

这篇关于与Symfony / Doctrine临时连接到外部数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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