Best method for an entity to detect if an embedded object was changed? #5471

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

Originally created by @BabakMN on GitHub (Mar 22, 2017).

Originally assigned to: @Ocramius on GitHub.

On a User entity with an embedded BillingDetails I'm using the following in preUpdate:

public function preUpdate(PreUpdateEventArgs $event) {

        $changeSet = $event->getEntityChangeset();

        $billingUpdated = false;
        foreach (array_keys($changeSet) as $changedField) {
            $key = 'billingDetails.';
            if (substr($changedField, 0, strlen($key)) === $key) {
                $billingUpdated = true;
                break;
            }
        }

        if ($billingUpdated) {
            $this->getBillingDetails()->setLastUpdated(new \DateTime('now'));
        }

}

It looks for at least one changed field that begins with billingDetails.. Is there more proper way of achieving this?

Edit: Fixed typo

Originally created by @BabakMN on GitHub (Mar 22, 2017). Originally assigned to: @Ocramius on GitHub. On a `User` entity with an embedded `BillingDetails` I'm using the following in `preUpdate`: ```php public function preUpdate(PreUpdateEventArgs $event) { $changeSet = $event->getEntityChangeset(); $billingUpdated = false; foreach (array_keys($changeSet) as $changedField) { $key = 'billingDetails.'; if (substr($changedField, 0, strlen($key)) === $key) { $billingUpdated = true; break; } } if ($billingUpdated) { $this->getBillingDetails()->setLastUpdated(new \DateTime('now')); } } ``` It looks for at least one changed field that begins with `billingDetails.`. Is there more proper way of achieving this? Edit: Fixed typo
admin added the Question label 2026-01-22 15:08:29 +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#5471