em = $entityManager; } public static function getSubscribedEvents() { return [ BeforeCrudActionEvent::class => ['setTranslatableLocale'] ]; } public function setTranslatableLocale(BeforeCrudActionEvent $event) { $entity = $event->getAdminContext()->getEntity()->getInstance(); $_locale = $event->getAdminContext()->getRequest()->get('_locale'); if($entity instanceof TranslatableInterface){ //parcours les champs association pour détecter un champ de association qui est traduisible foreach ($this->em->getClassMetadata(get_class($entity))->getAssociationMappings() as $associationMapping){ if (in_array(TranslatableInterface::class, class_implements($associationMapping["targetEntity"]))){ if($entity->__get($associationMapping['fieldName']) instanceof PersistentCollection){ foreach ($entity->__get($associationMapping['fieldName']) as $item){ $item->setTranslatableLocale($_locale); $this->em->refresh($item); } }else{ if($entity->__get($associationMapping['fieldName'])) { $entity->__get($associationMapping['fieldName'])->setTranslatableLocale($_locale); $this->em->refresh($entity->__get($associationMapping['fieldName'])); } } } } $entity->setTranslatableLocale($_locale); $this->em->refresh($entity); } } }