[PR #902] Fix Lifecycle Callbacks #8871

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

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

State: closed
Merged: Yes


Remove a bit of code that breaks lifecycle callbacks of parent MappedSuperclasses

Lets say we are working with this code:

/**
 * @MappedSuperclass
 */
abstract class BaseClass
{
    /**
     * @ORM\Column(name="last_modified", type="datetimetz", nullable=false)
     * @var \DateTime
     */
    protected $lastModified;

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setLastModifiedPreUpdate()
    {
        $this->setLastModified(new \DateTime());
    }

    /**
     * @param \DateTime $lastModified
     */
    public function setLastModified( $lastModified )
    {
        $this->lastModified = $lastModified;
    }

    /**
     * @return \DateTime
     */
    public function getLastModified()
    {
        return $this->lastModified;
    }
}

/**
 * Class MyClass
 *
 * @Entity
 * @HasLifecycleCallbacks
 */
class MyClass extends BaseClass
{
    // Whatever you want in here
}

And you want to save MyClass and have it call the Lifecycle callbacks for your BaseClass. If you leave things they way they are programmed today, the BaseClass will never get its callbacks registered.

By removing the little bit of code there [Lines 472-475], you now get lifecycle callbacks through the entire stack, not just the top most class.

**Original Pull Request:** https://github.com/doctrine/orm/pull/902 **State:** closed **Merged:** Yes --- Remove a bit of code that breaks lifecycle callbacks of parent MappedSuperclasses Lets say we are working with this code: ``` php /** * @MappedSuperclass */ abstract class BaseClass { /** * @ORM\Column(name="last_modified", type="datetimetz", nullable=false) * @var \DateTime */ protected $lastModified; /** * @ORM\PrePersist * @ORM\PreUpdate */ public function setLastModifiedPreUpdate() { $this->setLastModified(new \DateTime()); } /** * @param \DateTime $lastModified */ public function setLastModified( $lastModified ) { $this->lastModified = $lastModified; } /** * @return \DateTime */ public function getLastModified() { return $this->lastModified; } } /** * Class MyClass * * @Entity * @HasLifecycleCallbacks */ class MyClass extends BaseClass { // Whatever you want in here } ``` And you want to save MyClass and have it call the Lifecycle callbacks for your BaseClass. If you leave things they way they are programmed today, the BaseClass will never get its callbacks registered. By removing the little bit of code there [Lines 472-475], you now get lifecycle callbacks through the entire stack, not just the top most class.
admin added the pull-request label 2026-01-22 16:02:05 +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#8871