Hello,
This is in fact about 2 seperate questions.
I’m trying to implement some custom listener which is supposed to delete orphaned entites from a specific table.
- I am using the DI container to wire everything together, so how would I go about it here?
The doc states to register your listener in either the application or plugin class and then proceeds to provide an example. But in the example, the listener class apparently has no dependencies:
public function events(EventManagerInterface $eventManager): EventManagerInterface
{
$statistics = new UserStatistic();
$eventManager->on($statistics);
return $eventManager;
}
- My inital attempt was to use the table class as a listener, since it already implements
EventListenerInterfaceand the logic is strongly related to the table class, but to no avail. The registered callback is never triggered:
In controller
EventManager::instance()->dispatch(new Event('InvoiceDrafts.created', $this));
In table class
public function implementedEvents(): array
{
return array_merge(parent::implementedEvents(), [
'InvoiceDrafts.created' => 'cleanUp',
]);
}
[...]
public function cleanUp(EventInterface $event): int
{
return $this->deleteAll([
'buyer_id IS' => null,
'biller_id IS' => null,
]);
}
Why is this not working? I did not plan to create a behaviour here because apparently behaviours provide a convenient way to package up behavior that is common across many models. .
Thanks