Symfony3: How to set multiple connections?

Jotaeme

I'm working with a Symfony3 app and I want to set up multiple connections to different databases.

I've been looking around and I found out the documentation about entityManagers and DB connections. My config.yml is configured as follows:

config.yml

doctrine:
dbal:
    default_connection: default
    connections:
            default:
                    driver:   pdo_mysql
                    host:     "%database_host%"
                    port:     "%database_port%"
                    dbname:   "%database_name%"
                    user:     "%database_user%"
                    password: "%database_password%"
                    charset:  UTF8
                    mapping_types:
                      enum: string
            other:
                    driver:   pdo_mysql
                    host:     "%database_host2%"
                    port:     "%database_port2%"
                    dbname:   "%database_name2%"
                    user:     "%database_user2%"
                    password: "%database_password2%"
                    charset:  UTF8
                    mapping_types:
                      enum: string
orm:
    dql:
         string_functions:
                DAY:   DoctrineExtensions\Query\Mysql\Day
                MONTH: DoctrineExtensions\Query\Mysql\Month
                YEAR:  DoctrineExtensions\Query\Mysql\Year
    auto_generate_proxy_classes: "%kernel.debug%"
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true

So now I do can access to my database like this:

$con2 = $this->get('doctrine.dbal.other_connection');
$orders = $con2->fetchAll('SELECT * FROM orders');

But what I really need is to configure a second orm-mapping connection which will allow me to interact with entities instead of dealing with the second database directly. So again as the documentation says I added under the doctrine orm label:

orm:
    dql:
         string_functions:
                DAY:   DoctrineExtensions\Query\Mysql\Day
                MONTH: DoctrineExtensions\Query\Mysql\Month
                YEAR:  DoctrineExtensions\Query\Mysql\Year
    auto_generate_proxy_classes: "%kernel.debug%"
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true

    default_entity_manager: default
    entity_managers:
        default:
            connection: default
            mappings:
                AppBundle:  ~      
        other:
            connection: other
            mappings:
                OtherBundle: ~

This throws an exception:

ParseException in Parser.php line 296: Unable to parse at line 78 (near " entity_managers:").

How should I configure my config.yml to allow orm-mapping for my second database connection? Should I delete the dql label and use it only under a certain entity manager label?

d.garanzha

Try this one:

doctrine:
    orm:
        auto_generate_proxy_classes: true
        entity_managers:
            default:
                mappings:
                    AppBundle: ~
                naming_strategy: doctrine.orm.naming_strategy.underscore
                dql:
                   string_functions:
                       DAY: DoctrineExtensions\Query\Mysql\Day
            other:
                mappings:
                    OtherBundle: ~

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set up TcpListener to always listen and accept multiple connections?

From Dev

No adapter set exception when setting multiple connections in li3

From Dev

How to set parent id on a child entity when submitting symfony3 form

From Dev

How to "prioritise" multiple network connections

From Dev

set verbose in command Symfony3

From Dev

set verbose in command Symfony3

From Dev

How to send parameters in FormType for symfony3?

From Dev

How to auto generate a bundle in Symfony3?

From Dev

Multiple variable connections with Propel and Symfony2

From Dev

How to set application_name for postgres connections?

From Dev

How to set max_connections in MySQL Programmatically

From Dev

How to set max user connections in python environment?

From Dev

How to properly set the site with HTTPS for secure connections?

From Dev

How to create and hold multiple connections in Spring Integration

From Dev

How to process multiple connections simultaneously with HttpListener?

From Dev

How to deal with multiple connections from the same IP?

From Dev

How Atomic are Mysql Transactions given multiple connections?

From Dev

How to merge multiple Internet connections into one?

From Dev

How to handle multiple connections of the same user on Firebase?

From Dev

How to merge multiple Internet connections into one?

From Dev

How to create multiple schema connections using Java?

From Dev

How to handle multiple data connections at once

From Dev

How to use multiple database connections in laravel project?

From Dev

Codeigniter 3 multiple database connections with dbforge

From Dev

Symfony3, set user last login date

From Dev

Symfony3, set user last login date

From Dev

Using db arrays in set and include in symfony3

From Dev

Symfony3 using multiple AppKernel.php

From Dev

Symfony3 queryBuilder how to search with OR and how to search for substring?

Related Related

  1. 1

    How to set up TcpListener to always listen and accept multiple connections?

  2. 2

    No adapter set exception when setting multiple connections in li3

  3. 3

    How to set parent id on a child entity when submitting symfony3 form

  4. 4

    How to "prioritise" multiple network connections

  5. 5

    set verbose in command Symfony3

  6. 6

    set verbose in command Symfony3

  7. 7

    How to send parameters in FormType for symfony3?

  8. 8

    How to auto generate a bundle in Symfony3?

  9. 9

    Multiple variable connections with Propel and Symfony2

  10. 10

    How to set application_name for postgres connections?

  11. 11

    How to set max_connections in MySQL Programmatically

  12. 12

    How to set max user connections in python environment?

  13. 13

    How to properly set the site with HTTPS for secure connections?

  14. 14

    How to create and hold multiple connections in Spring Integration

  15. 15

    How to process multiple connections simultaneously with HttpListener?

  16. 16

    How to deal with multiple connections from the same IP?

  17. 17

    How Atomic are Mysql Transactions given multiple connections?

  18. 18

    How to merge multiple Internet connections into one?

  19. 19

    How to handle multiple connections of the same user on Firebase?

  20. 20

    How to merge multiple Internet connections into one?

  21. 21

    How to create multiple schema connections using Java?

  22. 22

    How to handle multiple data connections at once

  23. 23

    How to use multiple database connections in laravel project?

  24. 24

    Codeigniter 3 multiple database connections with dbforge

  25. 25

    Symfony3, set user last login date

  26. 26

    Symfony3, set user last login date

  27. 27

    Using db arrays in set and include in symfony3

  28. 28

    Symfony3 using multiple AppKernel.php

  29. 29

    Symfony3 queryBuilder how to search with OR and how to search for substring?

HotTag

Archive