2
2
3
3
namespace ShipMonk \DoctrineEntityPreloader ;
4
4
5
+ use ArrayAccess ;
5
6
use Doctrine \ORM \EntityManagerInterface ;
6
7
use Doctrine \ORM \Mapping \ClassMetadata ;
7
- use Doctrine \ORM \Mapping \ManyToManyAssociationMapping ;
8
- use Doctrine \ORM \Mapping \OneToManyAssociationMapping ;
9
- use Doctrine \ORM \Mapping \ToManyAssociationMapping ;
10
8
use Doctrine \ORM \PersistentCollection ;
11
9
use Doctrine \ORM \QueryBuilder ;
12
10
use LogicException ;
@@ -56,19 +54,19 @@ public function preload(
56
54
$ associationMapping = $ sourceClassMetadata ->getAssociationMapping ($ sourcePropertyName );
57
55
58
56
/** @var ClassMetadata<E> $targetClassMetadata */
59
- $ targetClassMetadata = $ this ->entityManager ->getClassMetadata ($ associationMapping-> targetEntity );
57
+ $ targetClassMetadata = $ this ->entityManager ->getClassMetadata ($ associationMapping[ ' targetEntity ' ] );
60
58
61
- if ($ associationMapping-> isIndexed ( )) {
59
+ if (isset ( $ associationMapping[ ' indexBy ' ] )) {
62
60
throw new LogicException ('Preloading of indexed associations is not supported ' );
63
61
}
64
62
65
63
$ maxFetchJoinSameFieldCount ??= 1 ;
66
64
$ sourceEntities = $ this ->loadProxies ($ sourceClassMetadata , $ sourceEntities , $ batchSize ?? self ::PRELOAD_ENTITY_DEFAULT_BATCH_SIZE , $ maxFetchJoinSameFieldCount );
67
65
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 ' ] }" ),
72
70
};
73
71
74
72
return $ preloader ($ sourceEntities , $ sourceClassMetadata , $ sourcePropertyName , $ targetClassMetadata , $ batchSize , $ maxFetchJoinSameFieldCount );
@@ -201,13 +199,9 @@ private function preloadToMany(
201
199
202
200
$ associationMapping = $ sourceClassMetadata ->getAssociationMapping ($ sourcePropertyName );
203
201
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 (...),
211
205
default => throw new LogicException ('Unsupported association mapping type ' ),
212
206
};
213
207
@@ -238,6 +232,7 @@ private function preloadToMany(
238
232
}
239
233
240
234
/**
235
+ * @param array<string, mixed>|ArrayAccess<string, mixed> $associationMapping
241
236
* @param ClassMetadata<S> $sourceClassMetadata
242
237
* @param ClassMetadata<T> $targetClassMetadata
243
238
* @param list<mixed> $uninitializedSourceEntityIdsChunk
@@ -248,7 +243,7 @@ private function preloadToMany(
248
243
* @template T of E
249
244
*/
250
245
private function preloadOneToManyInner (
251
- ToManyAssociationMapping $ associationMapping ,
246
+ array | ArrayAccess $ associationMapping ,
252
247
ClassMetadata $ sourceClassMetadata ,
253
248
ReflectionProperty $ sourceIdentifierReflection ,
254
249
string $ sourcePropertyName ,
@@ -272,7 +267,7 @@ private function preloadOneToManyInner(
272
267
$ targetPropertyName ,
273
268
$ uninitializedSourceEntityIdsChunk ,
274
269
$ maxFetchJoinSameFieldCount ,
275
- $ associationMapping-> orderBy () ,
270
+ $ associationMapping[ ' orderBy ' ] ?? [] ,
276
271
);
277
272
278
273
foreach ($ targetEntitiesList as $ targetEntity ) {
@@ -288,6 +283,7 @@ private function preloadOneToManyInner(
288
283
}
289
284
290
285
/**
286
+ * @param array<string, mixed>|ArrayAccess<string, mixed> $associationMapping
291
287
* @param ClassMetadata<S> $sourceClassMetadata
292
288
* @param ClassMetadata<T> $targetClassMetadata
293
289
* @param list<mixed> $uninitializedSourceEntityIdsChunk
@@ -298,7 +294,7 @@ private function preloadOneToManyInner(
298
294
* @template T of E
299
295
*/
300
296
private function preloadManyToManyInner (
301
- ToManyAssociationMapping $ associationMapping ,
297
+ array | ArrayAccess $ associationMapping ,
302
298
ClassMetadata $ sourceClassMetadata ,
303
299
ReflectionProperty $ sourceIdentifierReflection ,
304
300
string $ sourcePropertyName ,
@@ -309,7 +305,7 @@ private function preloadManyToManyInner(
309
305
int $ maxFetchJoinSameFieldCount ,
310
306
): array
311
307
{
312
- if (count ($ associationMapping-> orderBy () ) > 0 ) {
308
+ if (count ($ associationMapping[ ' orderBy ' ] ?? [] ) > 0 ) {
313
309
throw new LogicException ('Many-to-many associations with order by are not supported ' );
314
310
}
315
311
@@ -458,11 +454,11 @@ private function addFetchJoinsToPreventFetchDuringHydration(
458
454
}
459
455
460
456
/** @var ClassMetadata<E> $targetClassMetadata */
461
- $ targetClassMetadata = $ this ->entityManager ->getClassMetadata ($ associationMapping-> targetEntity );
457
+ $ targetClassMetadata = $ this ->entityManager ->getClassMetadata ($ associationMapping[ ' targetEntity ' ] );
462
458
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 ;
466
462
467
463
if (!$ isToOneInversed && !$ isToOneAbstract ) {
468
464
continue ;
0 commit comments