Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Cookbook
  4. Doctrine
  5. How to Work with multiple Entity Managers and Connections
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

How to Work with multiple Entity Managers and Connections

Edit this page

Warning: You are browsing the documentation for Symfony 2.4, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

How to Work with multiple Entity Managers and Connections

You can use multiple Doctrine entity managers or connections in a Symfony application. This is necessary if you are using different databases or even vendors with entirely different sets of entities. In other words, one entity manager that connects to one database will handle some entities while another entity manager that connects to another database might handle the rest.

Note

Using multiple entity managers is pretty easy, but more advanced and not usually required. Be sure you actually need multiple entity managers before adding in this layer of complexity.

The following configuration code shows how you can configure two entity managers:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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:   "%database_name2%"
                user:     "%database_user2%"
                password: "%database_password2%"
                charset:  UTF8

    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    AcmeDemoBundle:  ~
                    AcmeStoreBundle: ~
            customer:
                connection: customer
                mappings:
                    AcmeCustomerBundle: ~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="UTF-8"?>
<srv:container xmlns="http://symfony.com/schema/dic/doctrine"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:srv="http://symfony.com/schema/dic/services"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
                        http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">

    <config>
        <dbal default-connection="default">
            <connection name="default"
                driver="%database_driver%"
                host="%database_host%"
                port="%database_port%"
                dbname="%database_name%"
                user="%database_user%"
                password="%database_password%"
                charset="UTF8"
            />

            <connection name="customer"
                driver="%database_driver2%"
                host="%database_host2%"
                port="%database_port2%"
                dbname="%database_name2%"
                user="%database_user2%"
                password="%database_password2%"
                charset="UTF8"
            />
        </dbal>

        <orm default-entity-manager="default">
            <entity-manager name="default" connection="default">
                <mapping name="AcmeDemoBundle" />
                <mapping name="AcmeStoreBundle" />
            </entity-manager>

            <entity-manager name="customer" connection="customer">
                <mapping name="AcmeCustomerBundle" />
            </entity-manager>
        </orm>
    </config>
</srv:container>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$container->loadFromExtension('doctrine', array(
    'dbal' => array(
        'default_connection' => 'default',
        'connections' => array(
            'default' => array(
                'driver'   => '%database_driver%',
                'host'     => '%database_host%',
                'port'     => '%database_port%',
                'dbname'   => '%database_name%',
                'user'     => '%database_user%',
                'password' => '%database_password%',
                'charset'  => 'UTF8',
            ),
            'customer' => array(
                'driver'   => '%database_driver2%',
                'host'     => '%database_host2%',
                'port'     => '%database_port2%',
                'dbname'   => '%database_name2%',
                'user'     => '%database_user2%',
                'password' => '%database_password2%',
                'charset'  => 'UTF8',
            ),
        ),
    ),

    'orm' => array(
        'default_entity_manager' => 'default',
        'entity_managers' => array(
            'default' => array(
                'connection' => 'default',
                'mappings'   => array(
                    'AcmeDemoBundle'  => null,
                    'AcmeStoreBundle' => null,
                ),
            ),
            'customer' => array(
                'connection' => 'customer',
                'mappings'   => array(
                    'AcmeCustomerBundle' => null,
                ),
            ),
        ),
    ),
));

In this case, you've defined two entity managers and called them default and customer. The default entity manager manages entities in the AcmeDemoBundle and AcmeStoreBundle, while the customer entity manager manages entities in the AcmeCustomerBundle. You've also defined two connections, one for each entity manager.

Note

When working with multiple connections and entity managers, you should be explicit about which configuration you want. If you do omit the name of the connection or entity manager, the default (i.e. default) is used.

When working with multiple connections to create your databases:

1
2
3
4
5
# Play only with "default" connection
$ php app/console doctrine:database:create

# Play only with "customer" connection
$ php app/console doctrine:database:create --connection=customer

When working with multiple entity managers to update your schema:

1
2
3
4
5
# Play only with "default" mappings
$ php app/console doctrine:schema:update --force

# Play only with "customer" mappings
$ php app/console doctrine:schema:update --force --em=customer

If you do omit the entity manager's name when asking for it, the default entity manager (i.e. default) is returned:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class UserController extends Controller
{
    public function indexAction()
    {
        // All three return the "default" entity manager
        $em = $this->get('doctrine')->getManager();
        $em = $this->get('doctrine')->getManager('default');
        $em = $this->get('doctrine.orm.default_entity_manager');

        // Both of these return the "customer" entity manager
        $customerEm = $this->get('doctrine')->getManager('customer');
        $customerEm = $this->get('doctrine.orm.customer_entity_manager');
    }
}

You can now use Doctrine just as you did before - using the default entity manager to persist and fetch entities that it manages and the customer entity manager to persist and fetch its entities.

The same applies to repository calls:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class UserController extends Controller
{
    public function indexAction()
    {
        // Retrieves a repository managed by the "default" em
        $products = $this->get('doctrine')
            ->getRepository('AcmeStoreBundle:Product')
            ->findAll()
        ;

        // Explicit way to deal with the "default" em
        $products = $this->get('doctrine')
            ->getRepository('AcmeStoreBundle:Product', 'default')
            ->findAll()
        ;

        // Retrieves a repository managed by the "customer" em
        $customers = $this->get('doctrine')
            ->getRepository('AcmeCustomerBundle:Customer', 'customer')
            ->findAll()
        ;
    }
}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    No stress: we've got you covered with our 116 automated quality checks of your code

    No stress: we've got you covered with our 116 automated quality checks of your code

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Romain, a Symfony contributor

    Thanks Romain for being a Symfony contributor

    1 commit • 1 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia