|
2 | 2 |
|
3 | 3 | namespace W3C\LifecycleEventsBundle\EventListener;
|
4 | 4 |
|
5 |
| -use Doctrine\Common\Util\ClassUtils; |
6 |
| -use Doctrine\ORM\Event\LifecycleEventArgs; |
| 5 | +use Doctrine\ORM\Mapping\AssociationMapping; |
| 6 | +use Doctrine\Persistence\Event\LifecycleEventArgs; |
| 7 | +use Doctrine\ORM\Event\PostPersistEventArgs; |
7 | 8 | use Doctrine\ORM\Event\PreUpdateEventArgs;
|
8 | 9 | use Doctrine\ORM\Mapping\MappingException;
|
9 | 10 | use Doctrine\ORM\PersistentCollection;
|
@@ -41,22 +42,24 @@ public function __construct(LifecycleEventsDispatcher $dispatcher, AttributeGett
|
41 | 42 | /**
|
42 | 43 | * Called upon receiving postPersist events
|
43 | 44 | *
|
44 |
| - * @param LifecycleEventArgs $args event to feed the dispatcher with |
| 45 | + * @param PostPersistEventArgs $args event to feed the dispatcher with |
45 | 46 | *
|
46 | 47 | * @throws MappingException
|
47 | 48 | * @throws ReflectionException
|
48 | 49 | */
|
49 |
| - public function postPersist(LifecycleEventArgs $args): void |
| 50 | + public function postPersist(PostPersistEventArgs $args): void |
50 | 51 | {
|
51 | 52 | $entity = $args->getObject();
|
52 |
| - $class = ClassUtils::getRealClass(get_class($entity)); |
| 53 | + |
| 54 | + $classMetadata = $args->getObjectManager()->getClassMetadata($entity::class); |
| 55 | + $class = $classMetadata->getName(); |
| 56 | + |
53 | 57 | /** @var Create $attribute */
|
54 | 58 | $attribute = $this->attributeGetter->getAttribute($class, Create::class);
|
55 | 59 | if ($attribute) {
|
56 | 60 | $this->dispatcher->addCreation($attribute, $args);
|
57 | 61 | }
|
58 | 62 |
|
59 |
| - $classMetadata = $args->getObjectManager()->getClassMetadata($class); |
60 | 63 | foreach ($classMetadata->getAssociationMappings() as $property => $associationMapping) {
|
61 | 64 | if (!$classMetadata->isAssociationInverseSide($property)) {
|
62 | 65 | if ($classMetadata->isSingleValuedAssociation($property)) {
|
@@ -98,15 +101,15 @@ public function preSoftDelete(LifecycleEventArgs $args): void
|
98 | 101 | public function preRemove(LifecycleEventArgs $args): void
|
99 | 102 | {
|
100 | 103 | $entity = $args->getObject();
|
101 |
| - $class = ClassUtils::getRealClass(get_class($entity)); |
| 104 | + $classMetadata = $args->getObjectManager()->getClassMetadata($entity::class); |
| 105 | + $class = $classMetadata->getName(); |
102 | 106 |
|
103 | 107 | /** @var Delete $attribute */
|
104 | 108 | $attribute = $this->attributeGetter->getAttribute($class, Delete::class);
|
105 | 109 | if ($attribute) {
|
106 | 110 | $this->dispatcher->addDeletion($attribute, $args);
|
107 | 111 | }
|
108 | 112 |
|
109 |
| - $classMetadata = $args->getObjectManager()->getClassMetadata($class); |
110 | 113 | foreach ($classMetadata->getAssociationMappings() as $property => $associationMapping) {
|
111 | 114 | if (!$classMetadata->isAssociationInverseSide($property)) {
|
112 | 115 | if ($classMetadata->isSingleValuedAssociation($property)) {
|
@@ -135,7 +138,8 @@ public function preRemove(LifecycleEventArgs $args): void
|
135 | 138 | public function preUpdate(PreUpdateEventArgs $args): void
|
136 | 139 | {
|
137 | 140 | $entity = $args->getObject();
|
138 |
| - $class = ClassUtils::getRealClass(get_class($entity)); |
| 141 | + $classMetadata = $args->getObjectManager()->getClassMetadata($entity::class); |
| 142 | + $class = $classMetadata->getName(); |
139 | 143 |
|
140 | 144 | /** @var Update $attribute */
|
141 | 145 | $attribute = $this->attributeGetter->getAttribute($class, Update::class);
|
@@ -168,8 +172,8 @@ public function preUpdate(PreUpdateEventArgs $args): void
|
168 | 172 | */
|
169 | 173 | private function buildCollectionChanges(PreUpdateEventArgs $args, object $entity): array
|
170 | 174 | {
|
171 |
| - $realClass = ClassUtils::getRealClass(get_class($entity)); |
172 |
| - $classMetadata = $args->getObjectManager()->getClassMetadata($realClass); |
| 175 | + $classMetadata = $args->getObjectManager()->getClassMetadata($entity::class); |
| 176 | + $realClass = $classMetadata->getName(); |
173 | 177 | $collectionsChanges = [];
|
174 | 178 |
|
175 | 179 | /** @var PersistentCollection $u */
|
@@ -211,8 +215,9 @@ private function buildCollectionChanges(PreUpdateEventArgs $args, object $entity
|
211 | 215 | */
|
212 | 216 | private function buildChangeSet(PreUpdateEventArgs $args, $entity): array
|
213 | 217 | {
|
214 |
| - $realClass = ClassUtils::getRealClass(get_class($entity)); |
215 |
| - $classMetadata = $args->getObjectManager()->getClassMetadata($realClass); |
| 218 | + $classMetadata = $args->getObjectManager()->getClassMetadata($entity::class); |
| 219 | + $realClass = $classMetadata->getName(); |
| 220 | + |
216 | 221 | $changes = [];
|
217 | 222 | foreach (array_keys($args->getEntityChangeSet()) as $property) {
|
218 | 223 | $ignoreAnnotation = $this->attributeGetter->getPropertyAttribute(
|
@@ -290,19 +295,22 @@ private function propertyUpdateInverse(
|
290 | 295 | $mapping = $classMetadata->getAssociationMapping($property);
|
291 | 296 |
|
292 | 297 | if (isset($change['new']) && $change['new']) {
|
293 |
| - $newInverseMetadata = $em->getClassMetadata(ClassUtils::getRealClass(get_class($change['new']))); |
| 298 | + $newInverseMetadata = $em->getClassMetadata($change['new']::class); |
294 | 299 | } else {
|
295 | 300 | $newInverseMetadata = $em->getClassMetadata($mapping['targetEntity']);
|
296 | 301 | }
|
297 | 302 |
|
298 | 303 | if (isset($change['old']) && $change['old']) {
|
299 |
| - $oldInverseMetadata = $em->getClassMetadata(ClassUtils::getRealClass(get_class($change['old']))); |
| 304 | + $oldInverseMetadata = $em->getClassMetadata($change['old']::class); |
300 | 305 | } else {
|
301 | 306 | $oldInverseMetadata = $em->getClassMetadata($mapping['targetEntity']);
|
302 | 307 | }
|
303 | 308 |
|
304 | 309 | // Inverse side should always be similar for old and new entities, but in case that's not the case (because of
|
305 | 310 | // some weird inheritance, we consider old and new metadata
|
| 311 | + if (!isset($mapping['inversedBy'])) { |
| 312 | + return; |
| 313 | + } |
306 | 314 |
|
307 | 315 | // Old Inverse side is also single-valued (one-to-one)
|
308 | 316 | if ($oldInverseMetadata->isSingleValuedAssociation($mapping['inversedBy'])) {
|
@@ -333,14 +341,14 @@ private function updateOldInverse(
|
333 | 341 | ?object $oldEntity,
|
334 | 342 | object $owningEntity,
|
335 | 343 | LifecycleEventArgs $args,
|
336 |
| - array $mapping |
| 344 | + AssociationMapping $mapping |
337 | 345 | ): void {
|
338 | 346 | $inverseField = $mapping['inversedBy'] ?? null;
|
339 | 347 | if ($inverseField && $oldEntity) {
|
340 | 348 | $em = $args->getObjectManager();
|
341 | 349 |
|
342 |
| - $oldClass = ClassUtils::getRealClass(get_class($oldEntity)); |
343 |
| - $inverseMetadata = $em->getClassMetadata($oldClass); |
| 350 | + $inverseMetadata = $em->getClassMetadata($oldEntity::class); |
| 351 | + $oldClass = $inverseMetadata->getName(); |
344 | 352 |
|
345 | 353 | /** @var Update $targetAnnotation */
|
346 | 354 | $targetAnnotation = $this->attributeGetter->getAttribute($oldClass, Update::class);
|
@@ -389,14 +397,14 @@ private function updateNewInverse(
|
389 | 397 | ?object $newEntity,
|
390 | 398 | object $owningEntity,
|
391 | 399 | LifecycleEventArgs $args,
|
392 |
| - array $mapping |
| 400 | + AssociationMapping $mapping, |
393 | 401 | ): void {
|
394 | 402 | $inverseField = $mapping['inversedBy'] ?? null;
|
395 | 403 | if ($inverseField && $newEntity) {
|
396 | 404 | $em = $args->getObjectManager();
|
397 | 405 |
|
398 |
| - $newClass = ClassUtils::getRealClass(get_class($newEntity)); |
399 |
| - $inverseMetadata = $em->getClassMetadata($newClass); |
| 406 | + $inverseMetadata = $em->getClassMetadata($newEntity::class); |
| 407 | + $newClass = $inverseMetadata->getName(); |
400 | 408 |
|
401 | 409 | /** @var Update $targetAnnotation */
|
402 | 410 | $targetAnnotation = $this->attributeGetter->getAttribute($newClass, Update::class);
|
@@ -446,14 +454,14 @@ private function updateDeletedInverse(
|
446 | 454 | ?object $deletedEntity,
|
447 | 455 | object $owningEntity,
|
448 | 456 | LifecycleEventArgs $args,
|
449 |
| - array $mapping |
| 457 | + AssociationMapping $mapping |
450 | 458 | ): void {
|
451 | 459 | $inverseField = $mapping['inversedBy'] ?? null;
|
452 | 460 | if ($inverseField && $deletedEntity) {
|
453 | 461 | $em = $args->getObjectManager();
|
454 | 462 |
|
455 |
| - $deletedClass = ClassUtils::getRealClass(get_class($deletedEntity)); |
456 |
| - $inverseMetadata = $em->getClassMetadata($deletedClass); |
| 463 | + $inverseMetadata = $em->getClassMetadata($deletedEntity::class); |
| 464 | + $deletedClass = $inverseMetadata->getName(); |
457 | 465 |
|
458 | 466 | /** @var Update $targetAnnotation */
|
459 | 467 | $targetAnnotation = $this->attributeGetter->getAttribute($deletedClass, Update::class);
|
@@ -503,14 +511,14 @@ private function updateInsertedInverse(
|
503 | 511 | ?object $insertedEntity,
|
504 | 512 | object $owningEntity,
|
505 | 513 | LifecycleEventArgs $args,
|
506 |
| - array $mapping |
| 514 | + AssociationMapping $mapping, |
507 | 515 | ): void {
|
508 | 516 | $inverseField = $mapping['inversedBy'] ?? null;
|
509 | 517 | if ($inverseField && $insertedEntity) {
|
510 | 518 | $em = $args->getObjectManager();
|
511 | 519 |
|
512 |
| - $deletedClass = ClassUtils::getRealClass(get_class($insertedEntity)); |
513 |
| - $inverseMetadata = $em->getClassMetadata($deletedClass); |
| 520 | + $inverseMetadata = $em->getClassMetadata($insertedEntity::class); |
| 521 | + $deletedClass = $inverseMetadata->getName(); |
514 | 522 |
|
515 | 523 | /** @var Update $targetAnnotation */
|
516 | 524 | $targetAnnotation = $this->attributeGetter->getAttribute($deletedClass, Update::class);
|
|
0 commit comments