Symfony2:是否连接到其他数据库,是否没有所有其他功能? [英] Symfony2: Connect to additional database, without all the extras?

查看:88
本文介绍了Symfony2:是否连接到其他数据库,是否没有所有其他功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望连接到另一个数据库以运行一些任意查询,但不想使用实体和存储库等描述数据。 Symfony不会拥有该数据或对其进行任何管理。我只是在寻找有关如何的建议:

I'm looking to Connect to another database to run some arbitrary queries, but don't want to describe the data using Entities and Repositories and the like. Symfony will not "own" this data or manage anything about it. I'm simply looking for recommendations on how to:


  1. 将连接参数放在config.yml文件(和parameters.yml)中

  2. 使用Symfony组件(Doctrine?)进行连接以运行准备好的语句。

我可以找到的类似问题是使用Symfony / Doctrine与外部数据库的临时连接,但这似乎适用于Symfony 1,因为 Doctrine_Manager 在Symfony 2中不存在。

The only similar question I could find was Temporary Connection to External Database with Symfony/Doctrine, but that seems to apply to Symfony 1 since Doctrine_Manager doesn't exist in Symfony 2.

推荐答案

http://symfony.com/doc/current/cookbook/ doctrine / multiple_entity_managers.html

显示如何设置多个Doctrine 2 DBAL连接(忽略实体管理器部分)。

Shows how to setup multiple Doctrine 2 DBAL connections (ignore the entity manager part).

doctrine:
dbal:
    default_connection:   default
    connections:
        default:
            driver:   "%database_driver%"
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
        customer:
            driver:   "%database_driver2%"
            host:     "%database_host2%"
            port:     "%database_port2%"
            dbname:   customer
            user:     "%database_user2%"
            password: "%database_password2%"
            charset:  UTF8

这将产生一个称为doctrine的服务可以从服务容器中提取的.dbal.customer_connection。

This will yield a service called: doctrine.dbal.customer_connection which you can pull from the service container.

DBAL连接是围绕标准PHP PDO连接对象的薄包装。

The DBAL connection is a thin wrapper around the standard PHP PDO connection object.

http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference /data-retrieval-and-manipulation.html

显示如何使用连接。基本上与PDO对象相同。

Shows how to use the connection. Basically the same as the PDO object.

您还可以使用PDO对象本身来创建服务。我没有一个方便的例子,但是很容易做到。那将完全不需要教义2。

You could also just create a service using the PDO object itself. I don't have an example handy but it's easy enough to do. That would eliminate the need for Doctrine 2 completely.

这篇关于Symfony2:是否连接到其他数据库,是否没有所有其他功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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