Add interface for LifecycleEventArgs of PrePersist and PreUpdate #6717

Closed
opened 2026-01-22 15:37:28 +01:00 by admin · 2 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. - ...
admin closed this issue 2026-01-22 15:37:29 +01:00
Author
Owner

@beberlei commented on GitHub (May 13, 2021):

With PHP 8 this could be solved with union types. The two are completly different, so its good that they don't share an interface imho.

@beberlei commented on GitHub (May 13, 2021): With PHP 8 this could be solved with union types. The two are completly different, so its good that they don't share an interface imho.
Author
Owner

@VincentLanglet commented on GitHub (May 14, 2021):

With PHP 8 this could be solved with union types.

Not really, I'll just be able to type add typehint, but in order to use the hasChangedField method, I'll still need to add a instanceof PreUpdateEventArgs check.

The two are completly different, so its good that they don't share an interface imho.

I not fully understand why it's completely different.
If we consider changeset as "The value which need to be persisted/saved/updated" both could have a changeset.

All the value of the new entity which need to be persisted are in the changeset.
Only the change value of the entity updated are in the changeset.

@VincentLanglet commented on GitHub (May 14, 2021): > With PHP 8 this could be solved with union types. Not really, I'll just be able to type add typehint, but in order to use the `hasChangedField` method, I'll still need to add a `instanceof PreUpdateEventArgs` check. > The two are completly different, so its good that they don't share an interface imho. I not fully understand why it's **completely** different. If we consider changeset as "The value which need to be persisted/saved/updated" both could have a changeset. All the value of the new entity which need to be persisted are in the changeset. Only the change value of the entity updated are in the changeset.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6717