src/EventSubscriber/AdminSubscriber.php line 20

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Shop;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  7. class AdminSubscriber implements EventSubscriberInterface
  8. {
  9.     public static function getSubscribedEvents()
  10.     {
  11.         return [
  12.             BeforeEntityPersistedEvent::class => ['setCreatedAt'],
  13.             BeforeEntityUpdatedEvent::class => ['setUpdatedAt']
  14.         ];
  15.     }
  16.     public function setCreatedAt(BeforeEntityPersistedEvent $event)
  17.     {
  18.         $entityInstance $event->getEntityInstance();
  19.         if (!$entityInstance instanceof Shop) return;
  20.         $entityInstance->setCreatedAt(new \DateTime());
  21.         
  22.     }
  23.     public function setUpdatedAt(BeforeEntityUpdatedEvent $event)
  24.     {
  25.         $entityInstance $event->getEntityInstance();
  26.         if (!$entityInstance instanceof Shop) return;
  27.         $entityInstance->setUpdatedAt(new \DateTime());
  28.         
  29.     }
  30. }