[PR #1532] [DDC-3944] Add removeLifecycleCallback method in ClassMetadataInfo.php #9587

Open
opened 2026-01-22 16:04:46 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/1532

State: closed
Merged: No


Ability to remove a lifecycle callback.
This is useful in order to disable a callback method on specific case.

Example with TimeStampable and updateTimestamps callback method:

When a user connect to the website, some attributes get updated automatically like last_logged_time...etc.
The object will see it's updated_at changed which is correct from the object point of view.
But it is not correct from the functional point of view, because none updated that user object.

Example of a method that update the object but the updated_at update is not desired:

public function updateLoggedInDateTime(User $user)
{
    $user->setLastLoggedAt(new \DateTime());
    ...        
    // remove lifecycle methodes to not update updated_at field
    //         array:2 [▼
    //             "prePersist" => array:1 [▼
    //                 0 => "updateTimestamps"
    //             ]
    //             "preUpdate" => array:1 [▼
    //                 0 => "updateTimestamps"
    //             ]
    //         ]        
    $this->entityManager->getClassMetadata(get_class($user))->removeLifecycleCallback('updateTimestamps', 'prePersist');
    $this->entityManager->getClassMetadata(get_class($user))->removeLifecycleCallback('updateTimestamps', 'preUpdate');  


    $this->entityManager->transactional(function($em) use ($user) {
    $em->persist($user);
    });
}

So in this particular situation, there will be no date time updating in updated_at.

**Original Pull Request:** https://github.com/doctrine/orm/pull/1532 **State:** closed **Merged:** No --- Ability to remove a lifecycle callback. This is useful in order to disable a callback method on specific case. Example with TimeStampable and updateTimestamps callback method: When a user connect to the website, some attributes get updated automatically like last_logged_time...etc. The object will see it's updated_at changed which is correct from the object point of view. But it is not correct from the functional point of view, because none updated that user object. Example of a method that update the object but the updated_at update is not desired: ``` public function updateLoggedInDateTime(User $user) { $user->setLastLoggedAt(new \DateTime()); ... // remove lifecycle methodes to not update updated_at field // array:2 [▼ // "prePersist" => array:1 [▼ // 0 => "updateTimestamps" // ] // "preUpdate" => array:1 [▼ // 0 => "updateTimestamps" // ] // ] $this->entityManager->getClassMetadata(get_class($user))->removeLifecycleCallback('updateTimestamps', 'prePersist'); $this->entityManager->getClassMetadata(get_class($user))->removeLifecycleCallback('updateTimestamps', 'preUpdate'); $this->entityManager->transactional(function($em) use ($user) { $em->persist($user); }); } ``` So in this particular situation, there will be no date time updating in updated_at.
admin added the pull-request label 2026-01-22 16:04:46 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#9587