|
1 | 1 | .. index::
|
2 | 2 | single: Doctrine; Multiple entity managers
|
3 | 3 |
|
4 |
| -How to work with Multiple Entity Managers |
5 |
| -========================================= |
| 4 | +How to work with Multiple Entity Managers and Connections |
| 5 | +========================================================= |
6 | 6 |
|
7 |
| -You can use multiple entity managers in a Symfony2 application. This is |
8 |
| -necessary if you are using different databases or even vendors with entirely |
9 |
| -different sets of entities. In other words, one entity manager that connects |
10 |
| -to one database will handle some entities while another entity manager that |
11 |
| -connects to another database might handle the rest. |
| 7 | +You can use multiple Doctrine entity managers or connections in a Symfony2 |
| 8 | +application. This is necessary if you are using different databases or even |
| 9 | +vendors with entirely different sets of entities. In other words, one entity |
| 10 | +manager that connects to one database will handle some entities while another |
| 11 | +entity manager that connects to another database might handle the rest. |
12 | 12 |
|
13 | 13 | .. note::
|
14 | 14 |
|
@@ -23,6 +23,26 @@ The following configuration code shows how you can configure two entity managers
|
23 | 23 | .. code-block:: yaml
|
24 | 24 |
|
25 | 25 | doctrine:
|
| 26 | +dbal: |
| 27 | +default_connection: default |
| 28 | +connections: |
| 29 | +default: |
| 30 | +driver: %database_driver% |
| 31 | +host: %database_host% |
| 32 | +port: %database_port% |
| 33 | +dbname: %database_name% |
| 34 | +user: %database_user% |
| 35 | +password: %database_password% |
| 36 | +charset: UTF8 |
| 37 | +customer: |
| 38 | +driver: %database_driver2% |
| 39 | +host: %database_host2% |
| 40 | +port: %database_port2% |
| 41 | +dbname: %database_name2% |
| 42 | +user: %database_user2% |
| 43 | +password: %database_password2% |
| 44 | +charset: UTF8 |
| 45 | +
|
26 | 46 | orm:
|
27 | 47 | default_entity_manager: default
|
28 | 48 | entity_managers:
|
@@ -39,17 +59,34 @@ The following configuration code shows how you can configure two entity managers
|
39 | 59 | In this case, you've defined two entity managers and called them ``default``
|
40 | 60 | and ``customer``. The ``default`` entity manager manages entities in the
|
41 | 61 | ``AcmeDemoBundle`` and ``AcmeStoreBundle``, while the ``customer`` entity
|
42 |
| -manager manages entities in the ``AcmeCustomerBundle``. |
| 62 | +manager manages entities in the ``AcmeCustomerBundle``. You've also defined |
| 63 | +two connections, one for each entity manager. |
| 64 | + |
| 65 | +.. note:: |
| 66 | + |
| 67 | +When working with multiple connections and entity managers, you should be |
| 68 | +explicit about which configuration you want. If you *do* omit the name of |
| 69 | +the connection or entity manager, the default (i.e. ``default``) is used. |
| 70 | + |
| 71 | +When working with multiple connections to create your databases: |
| 72 | + |
| 73 | +.. code-block:: bash |
| 74 | +
|
| 75 | +# Play only with "default" connection |
| 76 | +$ php app/console doctrine:database:create |
| 77 | +
|
| 78 | +# Play only with "customer" connection |
| 79 | +$ php app/console doctrine:database:create --connection=customer |
| 80 | +
|
| 81 | +When working with multiple entity managers to update your schema: |
43 | 82 |
|
44 |
| -When working with multiple entity managers, you should be explicit about which |
45 |
| -entity manager you want. If you *do* omit the entity manager's name when you |
46 |
| -update your schema, the default (i.e. ``default``) is used:: |
| 83 | +.. code-block:: bash |
47 | 84 |
|
48 | 85 | # Play only with "default" mappings
|
49 |
| -php app/console doctrine:schema:update --force |
| 86 | +$ php app/console doctrine:schema:update --force |
50 | 87 |
|
51 | 88 | # Play only with "customer" mappings
|
52 |
| -php app/console doctrine:schema:update --force --em=customer |
| 89 | +$ php app/console doctrine:schema:update --force --em=customer |
53 | 90 |
|
54 | 91 | If you *do* omit the entity manager's name when asking for it,
|
55 | 92 | the default entity manager (i.e. ``default``) is returned::
|
|
0 commit comments