Skip to content

Backups and change detection

backup() clones an entity and stores the clone internally by default.

$user = $userManager->findOneBy(['id' => 42]);
$user->backup();

$user->setUsername('new-name');

if ($user->modifiedSinceBackup()) {
    $user->persist();
}

Retrieve the stored backup:

$original = $user->getBackup();

Create a clone without replacing the stored backup:

$temporaryCopy = $user->backup(dry: true);

Manually set one:

$user->setBackup(clone $user);

Change detection compares serialized entity values. Call backup() before editing; modifiedSinceBackup() throws when no backup exists.

Primary-key snapshots are managed separately by setOriginalPrimaryKeys() and are used by persistence when a loaded entity key changes.

Next chapter: Lifecycle hooks and bulk operations