|
12 | 12 | use Doctrine\ORM\PersistentCollection; |
13 | 13 | use Doctrine\ORM\QueryBuilder; |
14 | 14 | use LogicException; |
| 15 | +use ReflectionProperty; |
15 | 16 | use function array_chunk; |
16 | 17 | use function array_values; |
17 | 18 | use function count; |
18 | 19 | use function get_parent_class; |
19 | 20 | use function is_a; |
| 21 | +use function method_exists; |
20 | 22 |
|
21 | 23 | /** |
22 | 24 | * @template E of object |
@@ -123,7 +125,7 @@ private function loadProxies( |
123 | 125 | int $maxFetchJoinSameFieldCount, |
124 | 126 | ): array |
125 | 127 | { |
126 | | - $identifierAccessor = $classMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection |
| 128 | + $identifierAccessor = $this->getSingleIdPropertyAccessor($classMetadata); // e.g. Order::$id reflection |
127 | 129 | $identifierName = $classMetadata->getSingleIdentifierFieldName(); // e.g. 'id' |
128 | 130 |
|
129 | 131 | if ($identifierAccessor === null) { |
@@ -170,9 +172,9 @@ private function preloadToMany( |
170 | 172 | int $maxFetchJoinSameFieldCount, |
171 | 173 | ): array |
172 | 174 | { |
173 | | - $sourceIdentifierAccessor = $sourceClassMetadata->getSingleIdPropertyAccessor(); // e.g. Order::$id reflection |
174 | | - $sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Order::$items reflection |
175 | | - $targetIdentifierAccessor = $targetClassMetadata->getSingleIdPropertyAccessor(); |
| 175 | + $sourceIdentifierAccessor = $this->getSingleIdPropertyAccessor($sourceClassMetadata); // e.g. Order::$id reflection |
| 176 | + $sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Order::$items reflection |
| 177 | + $targetIdentifierAccessor = $this->getSingleIdPropertyAccessor($targetClassMetadata); |
176 | 178 |
|
177 | 179 | if ($sourceIdentifierAccessor === null || $sourcePropertyAccessor === null || $targetIdentifierAccessor === null) { |
178 | 180 | throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.'); |
@@ -253,17 +255,17 @@ private function preloadToMany( |
253 | 255 | private function preloadOneToManyInner( |
254 | 256 | array|ArrayAccess $associationMapping, |
255 | 257 | ClassMetadata $sourceClassMetadata, |
256 | | - PropertyAccessor $sourceIdentifierAccessor, |
| 258 | + PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor, |
257 | 259 | string $sourcePropertyName, |
258 | 260 | ClassMetadata $targetClassMetadata, |
259 | | - PropertyAccessor $targetIdentifierAccessor, |
| 261 | + PropertyAccessor|ReflectionProperty $targetIdentifierAccessor, |
260 | 262 | array $uninitializedSourceEntityIdsChunk, |
261 | 263 | array $uninitializedCollections, |
262 | 264 | int $maxFetchJoinSameFieldCount, |
263 | 265 | ): array |
264 | 266 | { |
265 | 267 | $targetPropertyName = $sourceClassMetadata->getAssociationMappedByTargetField($sourcePropertyName); // e.g. 'order' |
266 | | - $targetPropertyAccessor = $targetClassMetadata->getPropertyAccessor($targetPropertyName); // e.g. Item::$order reflection |
| 268 | + $targetPropertyAccessor = $this->getPropertyAccessor($targetClassMetadata, $targetPropertyName); // e.g. Item::$order reflection |
267 | 269 | $targetEntities = []; |
268 | 270 |
|
269 | 271 | if ($targetPropertyAccessor === null) { |
@@ -306,10 +308,10 @@ private function preloadOneToManyInner( |
306 | 308 | private function preloadManyToManyInner( |
307 | 309 | array|ArrayAccess $associationMapping, |
308 | 310 | ClassMetadata $sourceClassMetadata, |
309 | | - PropertyAccessor $sourceIdentifierAccessor, |
| 311 | + PropertyAccessor|ReflectionProperty $sourceIdentifierAccessor, |
310 | 312 | string $sourcePropertyName, |
311 | 313 | ClassMetadata $targetClassMetadata, |
312 | | - PropertyAccessor $targetIdentifierAccessor, |
| 314 | + PropertyAccessor|ReflectionProperty $targetIdentifierAccessor, |
313 | 315 | array $uninitializedSourceEntityIdsChunk, |
314 | 316 | array $uninitializedCollections, |
315 | 317 | int $maxFetchJoinSameFieldCount, |
@@ -389,7 +391,7 @@ private function preloadToOne( |
389 | 391 | int $maxFetchJoinSameFieldCount, |
390 | 392 | ): array |
391 | 393 | { |
392 | | - $sourcePropertyAccessor = $sourceClassMetadata->getPropertyAccessor($sourcePropertyName); // e.g. Item::$order reflection |
| 394 | + $sourcePropertyAccessor = $this->getPropertyAccessor($sourceClassMetadata, $sourcePropertyName); // e.g. Item::$order reflection |
393 | 395 |
|
394 | 396 | if ($sourcePropertyAccessor === null) { |
395 | 397 | throw new LogicException('Doctrine should use RuntimeReflectionService which never returns null.'); |
@@ -549,4 +551,31 @@ private function addFetchJoinsToPreventFetchDuringHydration( |
549 | 551 | } |
550 | 552 | } |
551 | 553 |
|
| 554 | + /** |
| 555 | + * @param ClassMetadata<object> $classMetadata |
| 556 | + */ |
| 557 | + private function getSingleIdPropertyAccessor(ClassMetadata $classMetadata): PropertyAccessor|ReflectionProperty|null |
| 558 | + { |
| 559 | + if (method_exists($classMetadata, 'getSingleIdPropertyAccessor')) { |
| 560 | + return $classMetadata->getSingleIdPropertyAccessor(); |
| 561 | + } |
| 562 | + |
| 563 | + return $classMetadata->getSingleIdReflectionProperty(); |
| 564 | + } |
| 565 | + |
| 566 | + /** |
| 567 | + * @param ClassMetadata<object> $classMetadata |
| 568 | + */ |
| 569 | + private function getPropertyAccessor( |
| 570 | + ClassMetadata $classMetadata, |
| 571 | + string $property, |
| 572 | + ): PropertyAccessor|ReflectionProperty|null |
| 573 | + { |
| 574 | + if (method_exists($classMetadata, 'getPropertyAccessor')) { |
| 575 | + return $classMetadata->getPropertyAccessor($property); |
| 576 | + } |
| 577 | + |
| 578 | + return $classMetadata->getReflectionProperty($property); |
| 579 | + } |
| 580 | + |
552 | 581 | } |
0 commit comments