src/EventListener/LoginListener.php line 20

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