src/EventListener/LoginListener.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by IntelliJ IDEA.
  4.  * User: jochen
  5.  * Date: 24.08.21
  6.  * Time: 15:40
  7.  */
  8. namespace App\EventListener;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  11. use App\Entity\User;
  12. class LoginListener
  13. {
  14.     private $em;
  15.     public function __construct(EntityManagerInterface $em)
  16.     {
  17.         $this->em $em;
  18.     }
  19.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  20.     {
  21.         // Get the User entity.
  22.         $user $event->getAuthenticationToken()->getUser();
  23.         // Update your field here.
  24.         $user->setLastLogin(new \DateTime());
  25.         // Persist the data to database.
  26.         $this->em->persist($user);
  27.         $this->em->flush();
  28.     }
  29. }