19
19
use Doctrine \ODM \MongoDB \Mapping \Annotations \Document ;
20
20
use Doctrine \ODM \MongoDB \Mapping \Driver \AttributeDriver ;
21
21
use Doctrine \Persistence \Mapping \Driver \MappingDriverChain ;
22
+ use Doctrine \Persistence \Proxy ;
22
23
use InvalidArgumentException ;
23
24
use MongoDB \Client ;
25
+ use ProxyManager \Proxy \LazyLoadingInterface ;
24
26
use Symfony \Bridge \Doctrine \DependencyInjection \AbstractDoctrineExtension ;
25
27
use Symfony \Bridge \Doctrine \Messenger \DoctrineClearEntityManagerWorkerSubscriber ;
26
28
use Symfony \Component \Cache \Adapter \ApcuAdapter ;
46
48
use function in_array ;
47
49
use function interface_exists ;
48
50
use function is_dir ;
51
+ use function method_exists ;
49
52
use function sprintf ;
50
53
51
54
/**
@@ -105,6 +108,11 @@ public function load(array $configs, ContainerBuilder $container): void
105
108
$ container ->removeDefinition ('doctrine_mongodb.odm.command.load_data_fixtures ' );
106
109
}
107
110
111
+ // Requires doctrine/mongodb-odm 2.10
112
+ $ useLazyGhostObject = method_exists (ODMConfiguration::class, 'setUseLazyGhostObject ' );
113
+ $ container ->getDefinition ('doctrine_mongodb ' )
114
+ ->setArgument (5 , $ useLazyGhostObject ? Proxy::class : LazyLoadingInterface::class);
115
+
108
116
// load the connections
109
117
$ this ->loadConnections ($ config ['connections ' ], $ container );
110
118
@@ -116,6 +124,7 @@ public function load(array $configs, ContainerBuilder $container): void
116
124
$ config ['default_document_manager ' ],
117
125
$ config ['default_database ' ],
118
126
$ container ,
127
+ $ useLazyGhostObject ,
119
128
);
120
129
121
130
if ($ config ['resolve_target_documents ' ]) {
@@ -197,7 +206,7 @@ protected function overrideParameters(array $options, ContainerBuilder $containe
197
206
* @param string $defaultDB The default db name
198
207
* @param ContainerBuilder $container A ContainerBuilder instance
199
208
*/
200
- protected function loadDocumentManagers (array $ dmConfigs , string |null $ defaultDM , string $ defaultDB , ContainerBuilder $ container ): void
209
+ protected function loadDocumentManagers (array $ dmConfigs , string |null $ defaultDM , string $ defaultDB , ContainerBuilder $ container, bool $ useLazyGhostObject = false ): void
201
210
{
202
211
$ dms = [];
203
212
foreach ($ dmConfigs as $ name => $ documentManager ) {
@@ -207,6 +216,7 @@ protected function loadDocumentManagers(array $dmConfigs, string|null $defaultDM
207
216
$ defaultDM ,
208
217
$ defaultDB ,
209
218
$ container ,
219
+ $ useLazyGhostObject ,
210
220
);
211
221
$ dms [$ name ] = sprintf ('doctrine_mongodb.odm.%s_document_manager ' , $ name );
212
222
}
@@ -222,7 +232,7 @@ protected function loadDocumentManagers(array $dmConfigs, string|null $defaultDM
222
232
* @param string $defaultDB The default db name
223
233
* @param ContainerBuilder $container A ContainerBuilder instance
224
234
*/
225
- protected function loadDocumentManager (array $ documentManager , string |null $ defaultDM , string $ defaultDB , ContainerBuilder $ container ): void
235
+ protected function loadDocumentManager (array $ documentManager , string |null $ defaultDM , string $ defaultDB , ContainerBuilder $ container, bool $ useLazyGhostObject = false ): void
226
236
{
227
237
$ connectionName = $ documentManager ['connection ' ] ?? $ documentManager ['name ' ];
228
238
$ configurationId = sprintf ('doctrine_mongodb.odm.%s_configuration ' , $ documentManager ['name ' ]);
@@ -254,10 +264,16 @@ protected function loadDocumentManager(array $documentManager, string|null $defa
254
264
'setPersistentCollectionDir ' => '%doctrine_mongodb.odm.persistent_collection_dir% ' ,
255
265
'setPersistentCollectionNamespace ' => '%doctrine_mongodb.odm.persistent_collection_namespace% ' ,
256
266
'setAutoGeneratePersistentCollectionClasses ' => '%doctrine_mongodb.odm.auto_generate_persistent_collection_classes% ' ,
257
- 'setUseLazyGhostObject ' => true ,
258
- 'setUseTransactionalFlush ' => $ documentManager ['use_transactional_flush ' ],
259
267
];
260
268
269
+ if ($ useLazyGhostObject ) {
270
+ $ methods ['setUseLazyGhostObject ' ] = $ useLazyGhostObject ;
271
+ }
272
+
273
+ if (method_exists (ODMConfiguration::class, 'setUseTransactionalFlush ' )) {
274
+ $ methods ['setUseTransactionalFlush ' ] = $ documentManager ['use_transactional_flush ' ];
275
+ }
276
+
261
277
if ($ documentManager ['repository_factory ' ]) {
262
278
$ methods ['setRepositoryFactory ' ] = new Reference ($ documentManager ['repository_factory ' ]);
263
279
}
0 commit comments