If I understand correctly, you have two Symfony applications - the first one with a database and the second one that uses this database via API. It's not possible to implement Doctrine events listener within the second application, as it's not aware of Doctrine entities of the first application. However, you can solve this problem by using Symfony Messenger Component. The first application would dispatch messages on every Doctrine event and the second application would subscribe to those messages and handle them.
Something like this.
The first application side.
<?php
declare(strict_types=1);
namespace AppEventListener;
use AppEntityUser;
use AppMessageUserChangedNotification;
use DoctrinePersistenceEventLifecycleEventArgs;
use SymfonyComponentMessengerMessageBusInterface;
class UserChangedNotifier
{
private MessageBusInterface $bus;
public function __construct(MessageBusInterface $bus)
{
$this->bus = $bus;
}
public function postUpdate(User $user, LifecycleEventArgs $event): void
{
$this->bus->dispatch(new UserChangedNotification($user));
}
}
The second application side.
<?php
declare(strict_types=1);
namespace AppMessageHandler;
use AppMessageUserChangedNotification;
use SymfonyComponentMessengerHandlerMessageHandlerInterface;
class UserChangedNotificationHandler implements MessageHandlerInterface
{
public function __invoke(UserChangedNotification $message)
{
// ... do something with the message, e.g. dispatch Symfony event
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…