Using database events

Overview

Events are the easiest way to extend the DataAbstractionLayer. Every entity comes with a set of events which will be dispatched in various situations.
All events are nested into one container event so that your subscriber should only get called once for e.g. a search request instead of dispatching the event 30 times.

Prerequisites

This guide is built upon the plugin base guide, but any plugin will work here. Just note that all examples are using the plugin mentioned above.
Furthermore you should have a look at our Listening to events guide since we are subscribing to events in this guide.

Event overview

The events below are dispatched for every entity in Shopware. The first part before the dot (.) equals your entity name. The examples are based on the product entity.
Event
Description
product.written
After the data has been written to storage
product.deleted
After the data has been deleted in storage
product.loaded
After the data has been hydrated into objects
product.search.result.loaded
After the search returned data
product.aggregation.result.loaded
After the aggregations have been loaded
product.id.search.result.loaded
After the search for ids only has been finished

product.written

The written event refers to Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent and provides the following information:
  • The reference class of the written definition
  • The data that was written
  • The context the data was written with
  • The list of affected primary keys
  • The list of errors if there are any

product.deleted

The deleted event refers to Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent and provides the following information:
  • The reference class of the deleted definition
  • The context the data was deleted with
  • The list of affected primary keys
  • The list of errors if there are any

product.loaded

The loaded event refers to Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent and provides the following information:
  • The reference class of the loaded definition
  • The context the data was loaded with
  • The list of hydrated entities

product.search.result.loaded

The loaded event refers to Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent and provides the following information:
  • The reference class of the loaded definition
  • The context the data was loaded with
  • The search result object including count, criteria and hydrated entities

product.aggregation.result.loaded

The loaded event refers to Shopware\Core\Framework\DataAbstractionLayer\Event\EntityAggregationResultLoadedEvent and provides the following information:
  • The results of the aggregation
  • The criteria the data was searched with
  • The context the data was loaded with

product.id.search.result.loaded

The loaded event refers to Shopware\Core\Framework\DataAbstractionLayer\Event\EntityIdSearchResultLoadedEvent and provides the following information:
  • The reference class of the loaded definition
  • The context the data was loaded with
  • The search result object including count, criteria, and list of ids

Event classes

All of stock entities come with their own event class. To keep the example of the product entity, you've got the ProductEvents class which is a list of constants to provide auto-completion and in case we are changing event names, you are covered.
The example below shows you how to use the constants in your event subscriber:
<plugin root>/src/Subscriber/ProductSubscriber.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Subscriber;
4
5
use Shopware\Core\Content\Product\ProductEvents;
6
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
7
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
class ProductSubscriber implements EventSubscriberInterface
11
{
12
public static function getSubscribedEvents()
13
{
14
return [
15
ProductEvents::PRODUCT_LOADED_EVENT => 'onLoad',
16
ProductEvents::PRODUCT_WRITTEN_EVENT => 'afterWrite',
17
];
18
}
19
20
public function onLoad(EntityLoadedEvent $event)
21
{
22
...
23
}
24
25
public function afterWrite(EntityWrittenEvent $event)
26
{
27
...
28
}
Copied!
After creating the event subscriber, you have to register it. If you don't know how that's done, head over to our guide about Listening to events.
Here's our services.xml:
<plugin root>/src/Resources/config/services.xml
1
<?xml version="1.0" ?>
2
3
<container xmlns="http://symfony.com/schema/dic/services"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6
7
<services>
8
<service id="Swag\BasicExample\Subscriber\ProductSubscriber">
9
<tag name="kernel.event_subscriber"/>
10
</service>
11
</services>
12
</container>
Copied!