I work with Redis loaded as a service to inject followers to a entity. So i've a entity like User that has a method like getFollowers. I don't want to mix service with entities, so I make a listener that subscribe to postLoad events in Doctrine.
The question is how call the service only when I call the getFollowers method.
My code...
EventListener:
public function postLoad(LifecycleEventArgs $eventArgs)
{
$redisService = get the service loaded with DIC in constructor.
if ($eventArgs->getEntity() instanceof User) {
$user = $eventArgs->getEntity();
$user->setFollowers($redisService->getFollowers($user));
}
}
User entity:
public function setFollowers(Array $followers) {
$this->followers = $followers
}
My problem is that on every load of class user, the RedisService is called and loaded, and I'd like to call the service ONLY on $user->getFollowers