@@ -47,6 +47,8 @@ public static function getSubscribedEvents()
4747
4848 /**
4949 * @param PrePersistEvent $prePersistEvent
50+ *
51+ * @throws \ReflectionException
5052 */
5153 public function onPrePersist (PrePersistEvent $ prePersistEvent )
5254 {
@@ -56,8 +58,8 @@ public function onPrePersist(PrePersistEvent $prePersistEvent)
5658 );
5759 if (isset ($ propertiesMetadata ['_id ' ])) {
5860 $ bulkOperationIndex = $ prePersistEvent ->getBulkOperationIndex ();
59- $ this ->entitiesData [$ bulkOperationIndex ]['entity ' ] = $ prePersistEvent ->getDocument ();
60- $ this ->entitiesData [$ bulkOperationIndex ]['metadata ' ] = $ propertiesMetadata ;
61+ $ this ->entitiesData [$ prePersistEvent -> getConnectionName ()][ $ bulkOperationIndex ]['entity ' ] = $ prePersistEvent ->getDocument ();
62+ $ this ->entitiesData [$ prePersistEvent -> getConnectionName ()][ $ bulkOperationIndex ]['metadata ' ] = $ propertiesMetadata ;
6163 }
6264 }
6365
@@ -66,7 +68,13 @@ public function onPrePersist(PrePersistEvent $prePersistEvent)
6668 */
6769 public function onPostCommit (PostCommitEvent $ postCommitEvent )
6870 {
69- foreach ($ this ->entitiesData as $ bulkOperationIndex => $ entityData ) {
71+ // No need to do anything if there are no persisted entities for that connection
72+ if (empty ($ this ->entitiesData [$ postCommitEvent ->getConnectionName ()])) {
73+ return ;
74+ }
75+
76+ // Update the ids of persisted entity objects
77+ foreach ($ this ->entitiesData [$ postCommitEvent ->getConnectionName ()] as $ bulkOperationIndex => $ entityData ) {
7078 $ idValue = current ($ postCommitEvent ->getBulkResponse ()['items ' ][$ bulkOperationIndex ])['_id ' ];
7179 $ idPropertyMetadata = $ entityData ['metadata ' ]['_id ' ];
7280 $ entity = $ entityData ['entity ' ];
@@ -78,6 +86,6 @@ public function onPostCommit(PostCommitEvent $postCommitEvent)
7886 }
7987
8088 // Clear the array to avoid any memory leaks
81- $ this ->entitiesData = [];
89+ $ this ->entitiesData [ $ postCommitEvent -> getConnectionName ()] = [];
8290 }
8391}
0 commit comments