Add interface for LifecycleEventArgs of PrePersist and PreUpdate #6715

Open
opened 2026-01-22 15:37:25 +01:00 by admin · 0 comments
Owner

Originally created by @VincentLanglet on GitHub (May 12, 2021).

I often use LifecycleCallback for both PrePersist and PreUpdate for similar thing.
A classic example would be

    /**
     * @ORM\PreUpdate
     */
    public function setFooOnUpdate(PreUpdateEventArgs $args): void
    {
        if ($args->hasChangedField('bar') && $this->getBar() === 1) {
            $this->setFoo(1);
        }
    }

    /**
     * @ORM\PrePersist
     */
    public function setFooOnPersist(): void
    {
        if ($this->getBar() === 1) {
            $this->setFoo(1);
        }
    }

I could write instead something like

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setFooOnPersist($args): void
    {
        if ($this->getBar() === 1 && (null === $this->getId() || $args instanceof PreUpdateEventArgs && $args->hasChangedField('bar'))) {
            $this->setFoo(1);
        }
    }

But the best would be

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setFooOnPersist(SomeInterface $args): void
    {
        if ($this->getBar() === 1 && $args->hasChangedField('bar')) {
            $this->setFoo(1);
        }
    }

In the case of PrePersist,

  • hasChangedField could return true.
    Maybe the same could be done for other method
  • getOldValue could return null.
  • getNewValue could return the new value.
  • ...
Originally created by @VincentLanglet on GitHub (May 12, 2021). I often use LifecycleCallback for both PrePersist and PreUpdate for similar thing. A classic example would be ``` /** * @ORM\PreUpdate */ public function setFooOnUpdate(PreUpdateEventArgs $args): void { if ($args->hasChangedField('bar') && $this->getBar() === 1) { $this->setFoo(1); } } /** * @ORM\PrePersist */ public function setFooOnPersist(): void { if ($this->getBar() === 1) { $this->setFoo(1); } } ``` I could write instead something like ``` /** * @ORM\PrePersist * @ORM\PreUpdate */ public function setFooOnPersist($args): void { if ($this->getBar() === 1 && (null === $this->getId() || $args instanceof PreUpdateEventArgs && $args->hasChangedField('bar'))) { $this->setFoo(1); } } ``` But the best would be ``` /** * @ORM\PrePersist * @ORM\PreUpdate */ public function setFooOnPersist(SomeInterface $args): void { if ($this->getBar() === 1 && $args->hasChangedField('bar')) { $this->setFoo(1); } } ``` In the case of PrePersist, - hasChangedField could return true. Maybe the same could be done for other method - getOldValue could return null. - getNewValue could return the new value. - ...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6715