Skip to content

Commit

Permalink
Fixes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
brysn committed Feb 23, 2023
1 parent bdcf80a commit 9843b02
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/Subscribers/DoctrineEncryptSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ protected function processFields(object $entity, EntityManagerInterface $em, boo

$unitOfWork = $em->getUnitOfWork();
$oid = spl_object_id($entity);
$meta = $em->getClassMetadata(get_class($entity));

foreach ($properties as $key => $refProperty) {
// Get the value in the entity.
Expand All @@ -180,15 +181,10 @@ protected function processFields(object $entity, EntityManagerInterface $em, boo
if (isset($changeSet[$key])) {
$encryptedValue = $this->encryptor->encrypt($value);
$refProperty->setValue($entity, $encryptedValue);
$unitOfWork->recomputeSingleEntityChangeSet($em->getClassMetadata(get_class($entity)), $entity);

if ($isInsert) {
// Restore the decrypted value after the change set update
$refProperty->setValue($entity, $value);
} else {
// Will be restored during postUpdate cycle
$this->rawValues[$oid][$key] = $value;
}
$unitOfWork->recomputeSingleEntityChangeSet($meta, $entity);

// Will be restored during postUpdate cycle for updates, or below for inserts
$this->rawValues[$oid][$key] = $value;
}
} else {
// Decryption is fired by onLoad and postFlush events.
Expand All @@ -200,7 +196,16 @@ protected function processFields(object $entity, EntityManagerInterface $em, boo
}
}

return !empty($properties);
if ($isInsert) {
// Restore the decrypted values after the change set update
foreach ($this->rawValues[$oid] as $prop => $rawValue) {
$refProperty = $meta->getReflectionProperty($prop);
$refProperty->setValue($entity, $rawValue);
}
unset($this->rawValues[$oid]);
}

return true;
}

public function postUpdate(LifecycleEventArgs $args): void
Expand Down

0 comments on commit 9843b02

Please sign in to comment.