Search code examples
shopwareshopware6

Shopware 6: How to 'tap in' to the update method of an entity?


I have tried to find a solution, but maybe I'm searching for the wrong term.

I want to modify an Entity (a custom one) when the update for the DAL is called. But I cannot seem to find what method to implement in my Entity. What I'm looking for is a kind of 'hook'. I thought that maybe I could create a Service and look for a specific event, but there is only events for Products, Categories etc. Should I implement an event for my Entity or is there something more general?


Solution

  • There is a general EntityWrittenEvent that is dispatched whenever the DAL writes an entity. When you want to listen to the written event for a specific entity you can do so by creating a EventSubscriber that listens on the {entity_name}.written event.

    The event classes for Products and Categories etc. are basically just constants for the same event, but the event name is generic. E.g instead of using ProductEvents::PRODUCT_WRITTEN_EVENT in your event subscriber you can listen on product.written as that is internally the same.

    Also take a look at the official docs there too product is used as an example, it works the same way for every entity (even custom ones).