|
1 | 1 | .. index:: |
2 | 2 | single: Doctrine; Multiple entity managers |
3 | 3 |
|
4 | | -How to Work with multiple Entity Managers and Connections |
| 4 | +How to Work with Multiple Entity Managers and Connections |
5 | 5 | ========================================================= |
6 | 6 |
|
7 | 7 | You can use multiple Doctrine entity managers or connections in a Symfony |
@@ -32,20 +32,12 @@ The following configuration code shows how you can configure two entity managers |
32 | 32 | # config/packages/doctrine.yaml |
33 | 33 | doctrine: |
34 | 34 | dbal: |
35 | | - default_connection: default |
36 | 35 | connections: |
37 | 36 | default: |
38 | | - # configure these for your database server |
39 | 37 | url: '%env(resolve:DATABASE_URL)%' |
40 | | - driver: 'pdo_mysql' |
41 | | - server_version: '5.7' |
42 | | - charset: utf8mb4 |
43 | 38 | customer: |
44 | | - # configure these for your database server |
45 | | - url: '%env(resolve:DATABASE_CUSTOMER_URL)%' |
46 | | - driver: 'pdo_mysql' |
47 | | - server_version: '5.7' |
48 | | - charset: utf8mb4 |
| 39 | + url: '%env(resolve:CUSTOMER_DATABASE_URL)%' |
| 40 | + default_connection: default |
49 | 41 | orm: |
50 | 42 | default_entity_manager: default |
51 | 43 | entity_managers: |
@@ -82,20 +74,12 @@ The following configuration code shows how you can configure two entity managers |
82 | 74 |
|
83 | 75 | <doctrine:config> |
84 | 76 | <doctrine:dbal default-connection="default"> |
85 | | - <!-- configure these for your database server --> |
86 | 77 | <doctrine:connection name="default" |
87 | 78 | url="%env(resolve:DATABASE_URL)%" |
88 | | - driver="pdo_mysql" |
89 | | - server_version="5.7" |
90 | | - charset="utf8mb4" |
91 | 79 | /> |
92 | 80 |
|
93 | | - <!-- configure these for your database server --> |
94 | 81 | <doctrine:connection name="customer" |
95 | | - url="%env(resolve:DATABASE_CUSTOMER_URL)%" |
96 | | - driver="pdo_mysql" |
97 | | - server_version="5.7" |
98 | | - charset="utf8mb4" |
| 82 | + url="%env(resolve:CUSTOMER_DATABASE_URL)%" |
99 | 83 | /> |
100 | 84 | </doctrine:dbal> |
101 | 85 |
|
@@ -131,37 +115,28 @@ The following configuration code shows how you can configure two entity managers |
131 | 115 | use Symfony\Config\DoctrineConfig; |
132 | 116 |
|
133 | 117 | return static function (DoctrineConfig $doctrine) { |
134 | | - $doctrine->dbal()->defaultConnection('default'); |
135 | | -
|
136 | | - // configure these for your database server |
| 118 | + // Connections: |
137 | 119 | $doctrine->dbal() |
138 | 120 | ->connection('default') |
139 | | - ->url(env('DATABASE_URL')->resolve()) |
140 | | - ->driver('pdo_mysql') |
141 | | - ->serverVersion('5.7') |
142 | | - ->charset('utf8mb4'); |
143 | | -
|
144 | | - // configure these for your database server |
| 121 | + ->url(env('DATABASE_URL')->resolve()); |
145 | 122 | $doctrine->dbal() |
146 | 123 | ->connection('customer') |
147 | | - ->url(env('DATABASE_CUSTOMER_URL')->resolve()) |
148 | | - ->driver('pdo_mysql') |
149 | | - ->serverVersion('5.7') |
150 | | - ->charset('utf8mb4'); |
151 | | -
|
| 124 | + ->url(env('CUSTOMER_DATABASE_URL')->resolve()); |
| 125 | + $doctrine->dbal()->defaultConnection('default'); |
| 126 | + |
| 127 | + // Entity Managers: |
152 | 128 | $doctrine->orm()->defaultEntityManager('default'); |
153 | | - $emDefault = $doctrine->orm()->entityManager('default'); |
154 | | - $emDefault->connection('default'); |
155 | | - $emDefault->mapping('Main') |
| 129 | + $defaultEntityManager = $doctrine->orm()->entityManager('default'); |
| 130 | + $defaultEntityManager->connection('default'); |
| 131 | + $defaultEntityManager->mapping('Main') |
156 | 132 | ->isBundle(false) |
157 | 133 | ->type('annotation') |
158 | 134 | ->dir('%kernel.project_dir%/src/Entity/Main') |
159 | 135 | ->prefix('App\Entity\Main') |
160 | 136 | ->alias('Main'); |
161 | | -
|
162 | | - $emCustomer = $doctrine->orm()->entityManager('customer'); |
163 | | - $emCustomer->connection('customer'); |
164 | | - $emCustomer->mapping('Customer') |
| 137 | + $customerEntityManager = $doctrine->orm()->entityManager('customer'); |
| 138 | + $customerEntityManager->connection('customer'); |
| 139 | + $customerEntityManager->mapping('Customer') |
165 | 140 | ->isBundle(false) |
166 | 141 | ->type('annotation') |
167 | 142 | ->dir('%kernel.project_dir%/src/Entity/Customer') |
|
0 commit comments