Symfony 2 access database of ms sql server and mysql at the same time

0

I work with symfony 2.5, doctrine ... I need to access two databases at once: one in mysql on a local server and one in ms sql server 2008 on a remote server. How do I configure the connections and the parameters to access both interchangeably? I even have to access the two databases from one bundle at a time. I need to configure 2 entity managers for each connection or with a vast one?

    
asked by Giselle Yanet 23.08.2017 в 01:58
source

1 answer

1

You will have to configure the two connections in your file config.yml

doctrine:
    dbal:
        default_connection: default
        connections:
            mysql:
                driver:   pdo_mysql
                host:     '%database_host%'
                port:     '%database_port%'
                dbname:   '%database_name%'
                user:     '%database_user%'
                password: '%database_password%'
                charset:  UTF8
            sqlserver:
                driver:   pdo_sqlsrv
                host:     '%database_host2%'
                port:     '%database_port2%'
                dbname:   '%database_name2%'
                user:     '%database_user2%'
                password: '%database_password2%'
                charset:  UTF8

    orm:
        default_entity_manager: mysql
        entity_managers:
            mysql:
                connection: mysql
                mappings:
                    AppBundle:  ~
                    AcmeStoreBundle: ~
            sqlserver:
                connection: sqlserver
                mappings:
                    AcmeCustomerBundle: ~

VERY IMPORTANT It is necessary that in the field mappings indicate which entities belong to each connection.

In the controller, or service, when you are going to use the EntityManager , you will have to indicate which connection to use. For example.

<?php
// dentro del controlador
$emMySQL = $this->getDoctrine()->getManager('mysql'); 
$emSQLServer = $this->getDoctrine()->getManager('sqlserver');

In the case of mysql , you could skip passing the name of the connection, since we would have configured it as the default connection here:

...
orm:
    default_entity_manager: mysql
...

UPDATE If all your entities are in the same bundle, you will have to configure it in the following way:

mysql:
  mappings:
    AppBundle:
      # you must specify the type
      type:     "annotation"    
      # The directory for entity (relative to bundle path)
      dir:      "Entity/MySQL"        
      #the prefix 
      prefix:   "Your\Bundle\Entity\MySQL" 

sqlserver:
  mappings:
    AppBundle:
      # you must specify the type
      type:     "annotation"    
      # The directory for entity (relative to bundle path)
      dir:      "Entity/SQLServer"        
      #the prefix 
      prefix:   "Your\Bundle\Entity\SQLServer"

You may have to alter the directory structure / namespaces you have right now.

    
answered by 23.08.2017 в 12:57