src/UserInterface/BackOffice/EventSubscriber/CustomerSubscriber.php line 19

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\UserInterface\BackOffice\EventSubscriber;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class CustomerSubscriber implements EventSubscriberInterface
  7. {
  8.     public static function getSubscribedEvents()
  9.     {
  10.         return [
  11.             BeforeEntityPersistedEvent::class => ['setCustomerInterlocutorForeignKey'],
  12.         ];
  13.     }
  14.     public function setCustomerInterlocutorForeignKey(BeforeEntityPersistedEvent $event)
  15.     {
  16.         $entity $event->getEntityInstance();
  17.         if (\Customer::class !== get_class($entity)) {
  18.             return;
  19.         }
  20.         foreach ($entity->getInterlocutors() as $interlocutor) {
  21.             $interlocutor->setCustomer($entity);
  22.         }
  23.     }
  24. }