I have a data structure including the models product
and version
, and each product
can be connected to a number of version
s.
The version
has a data point size
. On the web page, we want to show for each product
the maximum_size
, i.e., the maximum over the sizes of all connected version
s.
The idea to implement this efficiently in Typo3 12.4 is as follows:
maximum_size
in the data model of product
, compute and save the values based on existing version
s.version
object is saved, updated or removed from the database.product
which is connected to the new/updated/deleted version
and re-compute and save the value of maximum_size
.Are there Typo3 PSR-14 events which are already suitable for this application?
In particular, to my understanding of the documentation, those would be the candidates:
\TYPO3\CMS\Extbase\Event\Persistence\EntityPersistedEvent
\TYPO3\CMS\Extbase\Event\Persistence\EntityUpdatedInPersistenceEvent
\TYPO3\CMS\Extbase\Event\Persistence\EntityRemovedFromPersistenceEvent
I have tried the first one, but it does not seem to work:
# Classes/EventListener/ProductMaximumSizeUpdater.php
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\EventListener;
use \TYPO3\CMS\Extbase\Event\Persistence\EntityPersistedEvent;
use \Psr\Log\LoggerInterface;
final class ProductMaximumSizeUpdater
{
public function __construct(
private readonly LoggerInterface $logger,
) {}
public function __invoke(EntityPersistedEvent $event): void
{
$entity = $event->getObject();
$this->logger->info('test message');
}
}
# Configuration/Services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
MyVendor\MyExtension\EventListener\ProductMaximumSizeUpdater:
tags:
- name: event.listener
identifier: 'productMaximumSizeUpdater'
/var/log/ReplicareWiwaEventListener.log
:$GLOBALS['TYPO3_CONF_VARS']['LOG']['MyVendor]['MyExtension']['EventListener']['writerConfiguration'] = [
\TYPO3\CMS\Core\Log\LogLevel::INFO => [
\TYPO3\CMS\Core\Log\Writer\FileWriter::class => [
'logFile' => \TYPO3\CMS\Core\Core\Environment::getVarPath() . '/log/MyEventListener.log'
]
]
];
With this, I would expect, that whenever a new object is created and saved in the database in the backend of the web page, an info is logged to /var/log/MyEventListener.log
. But actually, the log file has not even been created (meaning, there was no logging at all).
Am I getting something wrong about how to implement the EventListener? Or is the EntityPersistedEvent
just not the right thing to use here?
Hi its Important to understand that TYPO3 Frontend and Backend use Different Methods for Persisting data.
the Backend Uses the Datahandler uses the classic hooks https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Events/Hooks/Index.html so you won't find any events for this right now.
and even in the Frotenend are different Possiblities of Persisting data. most modern Extensions Use Extbase which comes with its onwn ORM Logic and fires the Events you mentioned int your question.
but extensions might also use Querybuilder or other Database Classes. to modify Data directly. so to catch "all" updates you need to know the scope of your extension. does the editing only happen in the backend -> its most likly Datahandler hooks in the frontend the Evnents. if both you need to handle the logic for both.