Wolfone
Wolfone

Reputation: 1436

How to trigger postUpdate-event in doctrine for implicit entities?

I am trying to update a search-index whenever one of my doctrine-managed entities called Work in a symfony-7-project is updated. I use a postUpdate-Listener but I realized that the listener is not being executed when values exclusively in related entities change, which kinda makes sense as the actual table of my entity for which I registered the listener was technically not updated.

I feel though that there should be an easy solution for that.

#[AsEntityListener(
    event: Events::postUpdate,
    method: 'postUpdate',
    entity: Work::class,
)]
class PostUpdateIndexUpdate
{
    //constructor plus DI

    public function postUpdate(Work $work, PostUpdateEventArgs $eventArgs): void
    {
        //update logic plus logging which tells me if the listener was actually called
    }
}

I had the idea to add more AsEntityListener-Attributes, for each related entity...but that seems like a pretty bad idea as this might trigger a bunch of index-updates, which would:

Is it possible to catch all implicit updates more elegantly?

Upvotes: 1

Views: 389

Answers (1)

yceruto
yceruto

Reputation: 9575

In my case, the most convenient solution was adding/mapping a new timestamp field "updatedAt" to the aggregate root (here "Work") and then setting it up each time one of the related entities was modified. That way, Doctrine would trigger just one update event, and your listener would catch it.

I'm guessing here that all your sub-entities are being retrieved and modified through the Work entity, so you could update this timestamp field internally. Otherwise, you may work around a public setUpdatedAt() method to manually set this value from the use-case service.

Upvotes: 1

Related Questions