Help to create listener

Hi, I have an application and I would like to create a listener to listen for events referring to the select, insert, update and delete of the EventFeature, please does anyone have any ideas or suggestions on how to make this listener

Welcome to the forums! :slight_smile:

There’s a small example in the docs. Do you have any specific issues with creating the Listener?

Hello, @makxk I have read all the documentation, and I really did not understand how to create a listener to listen to the select, insert, update events.

If you have an example of how to create or create and add it to any application, it would help me a lot.

Thank you

This is only a short example to illustrate the basic usage.


The listener should be created as a separate class, which makes testing much easier.

use Laminas\Db\Sql\Insert;
use Laminas\Db\TableGateway\Feature\EventFeature;
use Laminas\Db\TableGateway\Feature\EventFeature\TableGatewayEvent;
use Laminas\EventManager\AbstractListenerAggregate;
use Laminas\EventManager\EventManagerInterface;

final class ExampleTableGatewayListener extends AbstractListenerAggregate
    public function attach(EventManagerInterface $events, $priority = 1): void
        $this->listeners[] = $events->attach(
    public function preInsert(TableGatewayEvent $event): void
        /** @var Insert $insert */
        $insert = $event->getParam('insert');

        // Modify values of the insert
                'my_colum' => '…',

Factory for Table Gateway

Add the listener to a table gateway class via factory.

use ExampleTableGatewayListener;
use Laminas\Db\TableGateway\Feature\EventFeature;
use Laminas\Db\TableGateway\Feature\FeatureSet;
use Psr\Container\ContainerInterface;

final class ExampleTableGatewayFactory
    public function __invoke(
        ContainerInterface $container
    ): ExampleTableGateway {
        // …

        // Event feature
        $eventFeature = new EventFeature();
        $eventManager = $eventFeature->getEventManager();

        // Listener
        /** @var ExampleTableGatewayListener $listener */
        $listener = $container->get(

        $featureSet = new FeatureSet();

        return new ExampleTableGateway(

@froschdesign This example should be on the doc site. Maybe in the cookbook? I learned more from this post about using the EventManager than I did from reading the docs. Greatly appreciated.

Unfortunately, I have too little time to take care of components that are no longer being developed.
For example, the entire Getting Started tutorial and all appendices need to be reworked.

And since this is not Slack, the example is not lost and is even under a category and has tags. :smiley: