|
9 | 9 | use Doctrine\ORM\PersistentCollection; |
10 | 10 | use Doctrine\ORM\QueryBuilder; |
11 | 11 | use LogicException; |
| 12 | +use ReflectionProperty; |
12 | 13 | use function array_chunk; |
13 | 14 | use function array_values; |
14 | 15 | use function count; |
15 | 16 | use function get_parent_class; |
16 | 17 | use function is_a; |
| 18 | +use function method_exists; |
17 | 19 |
|
18 | 20 | /** |
19 | 21 | * @template E of object |
@@ -120,7 +122,7 @@ private function loadProxies( |
120 | 122 | int $maxFetchJoinSameFieldCount, |
121 | 123 | ): array |
122 | 124 | { |
123 | | - $identifierAccessor = $classMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection |
| 125 | + $identifierAccessor = $this->getSingleIdPropertyAccessor($classMetadata); // e.g. Order::$id reflection |
124 | 126 | $identifierName = $classMetadata->getSingleIdentifierFieldName(); // e.g. 'id' |
125 | 127 |
|
126 | 128 | if ($identifierAccessor === null) { |
@@ -167,9 +169,9 @@ private function preloadToMany( |
167 | 169 | int $maxFetchJoinSameFieldCount, |
168 | 170 | ): array |
169 | 171 | { |
170 | | - $sourceIdentifierAccessor = $sourceClassMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection |
171 | | - $sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Order::$items reflection |
172 | | - $targetIdentifierAccessor = $targetClassMetadata->getSingleIdPropertyAccessor(); |
| 172 | + $sourceIdentifierAccessor = $this->getSingleIdPropertyAccessor($sourceClassMetadata); // e.g. Order::$id reflection |
| 173 | + $sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Order::$items reflection |
| 174 | + $targetIdentifierAccessor = $this->getSingleIdPropertyAccessor($targetClassMetadata); |
173 | 175 |
|
174 | 176 | if ($sourceIdentifierAccessor === null || $sourcePropertyAccessor === null || $targetIdentifierAccessor === null) { |
175 | 177 | throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.'); |
@@ -250,17 +252,17 @@ private function preloadToMany( |
250 | 252 | private function preloadOneToManyInner( |
251 | 253 | array|ArrayAccess $associationMapping, |
252 | 254 | ClassMetadata $sourceClassMetadata, |
253 | | - PropertyAccessor $sourceIdentifierAccessor, |
| 255 | + PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor, |
254 | 256 | string $sourcePropertyName, |
255 | 257 | ClassMetadata $targetClassMetadata, |
256 | | - PropertyAccessor $targetIdentifierAccessor, |
| 258 | + PropertyAccessor|ReflectionProperty $targetIdentifierAccessor, |
257 | 259 | array $uninitializedSourceEntityIdsChunk, |
258 | 260 | array $uninitializedCollections, |
259 | 261 | int $maxFetchJoinSameFieldCount, |
260 | 262 | ): array |
261 | 263 | { |
262 | 264 | $targetPropertyName = $sourceClassMetadata->getAssociationMappedByTargetField($sourcePropertyName); // e.g. 'order' |
263 | | - $targetPropertyAccessor = $targetClassMetadata->getPropertyAccessor($targetPropertyName); // e.g. Item::$order reflection |
| 265 | + $targetPropertyAccessor = $this->getPropertyAccessor($targetClassMetadata, $targetPropertyName); // e.g. Item::$order reflection |
264 | 266 | $targetEntities = []; |
265 | 267 |
|
266 | 268 | if ($targetPropertyAccessor === null) { |
@@ -302,10 +304,10 @@ private function preloadOneToManyInner( |
302 | 304 | private function preloadManyToManyInner( |
303 | 305 | array|ArrayAccess $associationMapping, |
304 | 306 | ClassMetadata $sourceClassMetadata, |
305 | | - PropertyAccessor $sourceIdentifierAccessor, |
| 307 | + PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor, |
306 | 308 | string $sourcePropertyName, |
307 | 309 | ClassMetadata $targetClassMetadata, |
308 | | - PropertyAccessor $targetIdentifierAccessor, |
| 310 | + PropertyAccessor|ReflectionProperty $targetIdentifierAccessor, |
309 | 311 | array $uninitializedSourceEntityIdsChunk, |
310 | 312 | array $uninitializedCollections, |
311 | 313 | int $maxFetchJoinSameFieldCount, |
@@ -379,7 +381,7 @@ private function preloadToOne( |
379 | 381 | int $maxFetchJoinSameFieldCount, |
380 | 382 | ): array |
381 | 383 | { |
382 | | - $sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Item::$order reflection |
| 384 | + $sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Item::$order reflection |
383 | 385 |
|
384 | 386 | if ($sourcePropertyAccessor === null) { |
385 | 387 | throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.'); |
@@ -483,4 +485,31 @@ private function addFetchJoinsToPreventFetchDuringHydration( |
483 | 485 | } |
484 | 486 | } |
485 | 487 |
|
| 488 | + /** |
| 489 | + * @param ClassMetadata<object> $classMetadata |
| 490 | + */ |
| 491 | + private function getSingleIdPropertyAccessor(ClassMetadata $classMetadata): PropertyAccessor|ReflectionProperty|null |
| 492 | + { |
| 493 | + if (method_exists($classMetadata, 'getSingleIdPropertyAccessor')) { |
| 494 | + return $classMetadata->getSingleIdPropertyAccessor(); |
| 495 | + } |
| 496 | + |
| 497 | + return $classMetadata->getSingleIdReflectionProperty(); |
| 498 | + } |
| 499 | + |
| 500 | + /** |
| 501 | + * @param ClassMetadata<object> $classMetadata |
| 502 | + */ |
| 503 | + private function getPropertyAccessor( |
| 504 | + ClassMetadata $classMetadata, |
| 505 | + string $property, |
| 506 | + ): PropertyAccessor|ReflectionProperty|null |
| 507 | + { |
| 508 | + if (method_exists($classMetadata, 'getPropertyAccessor')) { |
| 509 | + return $classMetadata->getPropertyAccessor($property); |
| 510 | + } |
| 511 | + |
| 512 | + return $classMetadata->getReflectionProperty($property); |
| 513 | + } |
| 514 | + |
486 | 515 | } |
0 commit comments