22
33namespace ShipMonk \DoctrineEntityPreloader ;
44
5+ use ArrayAccess ;
56use Doctrine \ORM \EntityManagerInterface ;
67use Doctrine \ORM \Mapping \ClassMetadata ;
7- use Doctrine \ORM \Mapping \ManyToManyAssociationMapping ;
8- use Doctrine \ORM \Mapping \OneToManyAssociationMapping ;
9- use Doctrine \ORM \Mapping \ToManyAssociationMapping ;
108use Doctrine \ORM \PersistentCollection ;
119use Doctrine \ORM \QueryBuilder ;
1210use LogicException ;
@@ -56,19 +54,19 @@ public function preload(
5654 $ associationMapping = $ sourceClassMetadata ->getAssociationMapping ($ sourcePropertyName );
5755
5856 /** @var ClassMetadata<E> $targetClassMetadata */
59- $ targetClassMetadata = $ this ->entityManager ->getClassMetadata ($ associationMapping-> targetEntity );
57+ $ targetClassMetadata = $ this ->entityManager ->getClassMetadata ($ associationMapping[ ' targetEntity ' ] );
6058
61- if ($ associationMapping-> isIndexed ( )) {
59+ if (isset ( $ associationMapping[ ' indexBy ' ] )) {
6260 throw new LogicException ('Preloading of indexed associations is not supported ' );
6361 }
6462
6563 $ maxFetchJoinSameFieldCount ??= 1 ;
6664 $ sourceEntities = $ this ->loadProxies ($ sourceClassMetadata , $ sourceEntities , $ batchSize ?? self ::PRELOAD_ENTITY_DEFAULT_BATCH_SIZE , $ maxFetchJoinSameFieldCount );
6765
68- $ preloader = match (true ) {
69- $ associationMapping -> isToOne () => $ this ->preloadToOne (...),
70- $ associationMapping -> isToMany () => $ this ->preloadToMany (...),
71- default => throw new LogicException ("Unsupported association mapping type {$ associationMapping-> type () }" ),
66+ $ preloader = match ($ associationMapping [ ' type ' ] ) {
67+ ClassMetadata:: ONE_TO_ONE , ClassMetadata:: MANY_TO_ONE => $ this ->preloadToOne (...),
68+ ClassMetadata:: ONE_TO_MANY , ClassMetadata:: MANY_TO_MANY => $ this ->preloadToMany (...),
69+ default => throw new LogicException ("Unsupported association mapping type {$ associationMapping[ ' type ' ] }" ),
7270 };
7371
7472 return $ preloader ($ sourceEntities , $ sourceClassMetadata , $ sourcePropertyName , $ targetClassMetadata , $ batchSize , $ maxFetchJoinSameFieldCount );
@@ -201,13 +199,9 @@ private function preloadToMany(
201199
202200 $ associationMapping = $ sourceClassMetadata ->getAssociationMapping ($ sourcePropertyName );
203201
204- if (!$ associationMapping instanceof ToManyAssociationMapping) {
205- throw new LogicException ('Unsupported association mapping type ' );
206- }
207-
208- $ innerLoader = match (true ) {
209- $ associationMapping instanceof OneToManyAssociationMapping => $ this ->preloadOneToManyInner (...),
210- $ associationMapping instanceof ManyToManyAssociationMapping => $ this ->preloadManyToManyInner (...),
202+ $ innerLoader = match ($ associationMapping ['type ' ]) {
203+ ClassMetadata::ONE_TO_MANY => $ this ->preloadOneToManyInner (...),
204+ ClassMetadata::MANY_TO_MANY => $ this ->preloadManyToManyInner (...),
211205 default => throw new LogicException ('Unsupported association mapping type ' ),
212206 };
213207
@@ -238,6 +232,7 @@ private function preloadToMany(
238232 }
239233
240234 /**
235+ * @param array<string, mixed>|ArrayAccess<string, mixed> $associationMapping
241236 * @param ClassMetadata<S> $sourceClassMetadata
242237 * @param ClassMetadata<T> $targetClassMetadata
243238 * @param list<mixed> $uninitializedSourceEntityIdsChunk
@@ -248,7 +243,7 @@ private function preloadToMany(
248243 * @template T of E
249244 */
250245 private function preloadOneToManyInner (
251- ToManyAssociationMapping $ associationMapping ,
246+ array | ArrayAccess $ associationMapping ,
252247 ClassMetadata $ sourceClassMetadata ,
253248 ReflectionProperty $ sourceIdentifierReflection ,
254249 string $ sourcePropertyName ,
@@ -272,7 +267,7 @@ private function preloadOneToManyInner(
272267 $ targetPropertyName ,
273268 $ uninitializedSourceEntityIdsChunk ,
274269 $ maxFetchJoinSameFieldCount ,
275- $ associationMapping-> orderBy () ,
270+ $ associationMapping[ ' orderBy ' ] ?? [] ,
276271 );
277272
278273 foreach ($ targetEntitiesList as $ targetEntity ) {
@@ -288,6 +283,7 @@ private function preloadOneToManyInner(
288283 }
289284
290285 /**
286+ * @param array<string, mixed>|ArrayAccess<string, mixed> $associationMapping
291287 * @param ClassMetadata<S> $sourceClassMetadata
292288 * @param ClassMetadata<T> $targetClassMetadata
293289 * @param list<mixed> $uninitializedSourceEntityIdsChunk
@@ -298,7 +294,7 @@ private function preloadOneToManyInner(
298294 * @template T of E
299295 */
300296 private function preloadManyToManyInner (
301- ToManyAssociationMapping $ associationMapping ,
297+ array | ArrayAccess $ associationMapping ,
302298 ClassMetadata $ sourceClassMetadata ,
303299 ReflectionProperty $ sourceIdentifierReflection ,
304300 string $ sourcePropertyName ,
@@ -309,7 +305,7 @@ private function preloadManyToManyInner(
309305 int $ maxFetchJoinSameFieldCount ,
310306 ): array
311307 {
312- if (count ($ associationMapping-> orderBy () ) > 0 ) {
308+ if (count ($ associationMapping[ ' orderBy ' ] ?? [] ) > 0 ) {
313309 throw new LogicException ('Many-to-many associations with order by are not supported ' );
314310 }
315311
@@ -458,11 +454,11 @@ private function addFetchJoinsToPreventFetchDuringHydration(
458454 }
459455
460456 /** @var ClassMetadata<E> $targetClassMetadata */
461- $ targetClassMetadata = $ this ->entityManager ->getClassMetadata ($ associationMapping-> targetEntity );
457+ $ targetClassMetadata = $ this ->entityManager ->getClassMetadata ($ associationMapping[ ' targetEntity ' ] );
462458
463- $ isToOne = ($ associationMapping-> type () & ClassMetadata::TO_ONE ) !== 0 ;
464- $ isToOneInversed = $ isToOne && ! $ associationMapping-> isOwningSide () ;
465- $ isToOneAbstract = $ isToOne && $ associationMapping-> isOwningSide () && count ($ targetClassMetadata ->subClasses ) > 0 ;
459+ $ isToOne = ($ associationMapping[ ' type ' ] & ClassMetadata::TO_ONE ) !== 0 ;
460+ $ isToOneInversed = $ isToOne && $ associationMapping[ ' isOwningSide ' ] === false ;
461+ $ isToOneAbstract = $ isToOne && $ associationMapping[ ' isOwningSide ' ] === true && count ($ targetClassMetadata ->subClasses ) > 0 ;
466462
467463 if (!$ isToOneInversed && !$ isToOneAbstract ) {
468464 continue ;
0 commit comments