Symfony2:连接到额外的数据库,没有所有额外的东西? [英] Symfony2: Connect to additional database, without all the extras?

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

问题描述

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. Put the connection parameters in my config.yml file (and parameters.yml)
  2. Connect do it using Symfony components (Doctrine?) to run prepared statements.

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

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

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

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

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

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天全站免登陆