DamienHarper / auditor-bundle

The missing audit log library
MIT License
399 stars 121 forks source link

Entity metadata cannot be loaded with multiple entity managers #413

Open Lehren opened 6 months ago

Lehren commented 6 months ago
Q A
auditor-bundle version 5.2.3
PHP version 8.0.30
Database MySQL

Summary

After updating, I get an error saying that an entity was not found in the chain configured namespaces. This seems to be because I have two entity managers in doctrine.yml that each have separate mappings. These entity managers are also registered as auditing providers. Yet in the code in Provider/Doctrine/Configuration.php all entities are processed for each auditing service, which cannot possibly work.

Doctrine.yml:

doctrine:
  orm:
    default_entity_manager: default
    entity_managers:
      default:
        connection: default
        mappings:
          FirstBundle: ~
      special:
        connection: special
        mappings:
          SecondBundle: ~
            foreach ($auditingServices as $auditingService) {
                $entityManager = $auditingService->getEntityManager();
                $platform = $entityManager->getConnection()->getDatabasePlatform();

                ...

                \assert(null !== $this->entities);
                foreach ($this->entities as $entity => $config) {

                    //Below line fails because an entity is not mapped for the current auditingService/entityManager
                    $meta = $entityManager->getClassMetadata(DoctrineHelper::getRealClassName($entity));

Current behavior

Loading entities crashes and auditing changes therefore does not work

How to reproduce

Have two entity managers setup in doctrine.yml. Each having different entity mappings. Register the entity managers are auditingProviders and try to edit some entities. It will crash on flush because it is trying to load an entity metadata with an entityManager it is not mapped for.

Expected behavior

It should load entities that are mapped to the relevant entityManager, and skip an entityManager if it does not have the correct mapping.

As a workaround I can register every entity to every entityManager, but that is sloppy and bloats the code