Roave / psr-container-doctrine

Doctrine Factories for PSR-11 Containers
BSD 2-Clause "Simplified" License
95 stars 31 forks source link

Functional config example for doctrine with mezzio #49

Open RodriAndreotti opened 3 years ago

RodriAndreotti commented 3 years ago

Hello, there! I'm struggling for 3 days to configure the roave/psr-container-doctrine with mezzio.

First I struggled with caches, then with MappingDriver, now I managed to make it all work, but it doesn't recognize entities. This is my exception:

Doctrine\ORM\Mapping\MappingException: Class "Usuario\Entity\Usuario" is not a valid entity or mapped super class. in file /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php on line 378

Stack trace:
  1. Doctrine\ORM\Mapping\MappingException->() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:378
  2. Doctrine\ORM\Mapping\MappingException->classIsNotAValidEntityOrMappedSuperClass() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php:101
  3. Doctrine\ORM\Mapping\Driver\AnnotationDriver->loadMetadataForClass() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/Driver/MappingDriverChain.php:79
  4. Doctrine\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:156
  5. Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:382
  6. Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:85
  7. Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:251
  8. Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:293
  9. Doctrine\ORM\EntityManager->getClassMetadata() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php:45
 10. Doctrine\ORM\Repository\DefaultRepositoryFactory->getRepository() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:768
 11. Doctrine\ORM\EntityManager->getRepository() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/src/Usuario/src/Repository/UsuarioRepository.php:23
 12. Usuario\Repository\UsuarioRepository->getByLogin() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/src/Usuario/src/Middleware/LoginMiddleware.php:53
 13. Usuario\Middleware\LoginMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 14. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-router/src/Route.php:85
 15. Mezzio\Router\Route->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-router/src/RouteResult.php:97
 16. Mezzio\Router\RouteResult->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-router/src/Middleware/DispatchMiddleware.php:30
 17. Mezzio\Router\Middleware\DispatchMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 18. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 19. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-helpers/src/UrlHelperMiddleware.php:48
 20. Mezzio\Helper\UrlHelperMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 21. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 22. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-router/src/Middleware/MethodNotAllowedMiddleware.php:56
 23. Mezzio\Router\Middleware\MethodNotAllowedMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 24. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 25. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-router/src/Middleware/ImplicitOptionsMiddleware.php:72
 26. Mezzio\Router\Middleware\ImplicitOptionsMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 27. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 28. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-router/src/Middleware/ImplicitHeadMiddleware.php:76
 29. Mezzio\Router\Middleware\ImplicitHeadMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 30. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 31. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-router/src/Middleware/RouteMiddleware.php:50
 32. Mezzio\Router\Middleware\RouteMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 33. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 34. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Middleware/PathMiddlewareDecorator.php:43
 35. Laminas\Stratigility\Middleware\PathMiddlewareDecorator->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 36. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-helpers/src/BodyParams/BodyParamsMiddleware.php:83
 37. Mezzio\Helper\BodyParams\BodyParamsMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 38. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 39. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio-helpers/src/ServerUrlMiddleware.php:36
 40. Mezzio\Helper\ServerUrlMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 41. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 42. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/tuupola/cors-middleware/src/CorsMiddleware.php:124
 43. Tuupola\Middleware\CorsMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 44. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Middleware/ErrorHandler.php:131
 45. Laminas\Stratigility\Middleware\ErrorHandler->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Middleware/LazyLoadingMiddleware.php:37
 46. Mezzio\Middleware\LazyLoadingMiddleware->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/Next.php:51
 47. Laminas\Stratigility\Next->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/MiddlewarePipe.php:76
 48. Laminas\Stratigility\MiddlewarePipe->process() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-stratigility/src/MiddlewarePipe.php:65
 49. Laminas\Stratigility\MiddlewarePipe->handle() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/laminas/laminas-httphandlerrunner/src/RequestHandlerRunner.php:96
 50. Laminas\HttpHandlerRunner\RequestHandlerRunner->run() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/private/vendor/mezzio/mezzio/src/Application.php:68
 51. Mezzio\Application->run() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/public/index.php:30
 52. {closure}() /mnt/arquivos/Projetos/Neocyber/NeoSchool/Backend/public/index.php:31

Also (but less important) when I try to validate the schema on cli it tells me that the enum type is not supported.

Could someone give me some help?

My actual doctrine.global.config:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

return array(
    'doctrine' => [
        'driver'    =>  [
            'orm_default'   =>  [
                'class' => \Doctrine\Persistence\Mapping\Driver\MappingDriverChain::class,
            ]
        ],
        'connection' => [
            'orm_default' => [
                'driverClass' => \Doctrine\DBAL\Driver\PDOMySql\Driver::class,
                'charset' => 'UTF8mb4',
                'collation' => 'UTF8mb4_unicode_ci',
            ]
        ],
        'configuration' => [
            'orm_default' => [
                'result_cache' => 'custom_cache',
                'metadata_cache' => 'custom_cache',
                'query_cache' => 'custom_cache',
                'hydration_cache' => 'custom_cache',
            ]
        ],
        'cache' => [
            'custom_cache' => [
                'class' => \Common\Cache\CustomCache::class, // Because Doctrine Cache breaking changes
            ],
        ],
        'types' => [
            'enum'  => Doctrine\DBAL\Types\StringType::class
        ]
    ],
    'dependencies' => [
        'factories' => [
            'doctrine.entity_manager.orm_default' => \Roave\PsrContainerDoctrine\EntityManagerFactory::class,
        ],
    ]
);

Relevant part of ConfigProvider.php:


/**
     * Retorna configuração das Entities do doctrine
     * @return array
     */
    public function getEntities()
    {
        return array(
            'driver' => [
                'orm_default' => [
                    'drivers' => [
                        Entity\Usuario::class => 'usuario_entity',
                    ],
                ], 
                // Here I tried several possible ways and configuration structures for the driver and paths, but with no result.
                'usuario_entity' => [
                    'class' => AnnotationDriver::class,
                    'cache' =>  'custom_cache',
                    'paths' => __DIR__ . '/Entity',
                ],
            ],
        );
    }
...

If anyone can help me figure out where I'm going wrong I'll be very grateful. I use a configuration very similar to this one in another API with Zend Expressive and with the DASPRiD/container-interop-doctrine plugin, and there it works perfectly.

But here I haven't been able to find the way yet.

Thanks in advance.

RodriAndreotti commented 3 years ago

OK, I think I managed to solve the problem. I'll leave it registered in case someone else finds the same case as mine:

A little bit of context, as the doctrine/cache drivers are obsolete and have been removed from version 2 of the package, I had to create a new "fake" driver in order to get psr-container-doctrine to work properly, done that my code started to show the above errors, after much debugging in the doctrine's internal codes, I decided to take a look at DriverFactory's psr-container-doctrine, and that's when I noticed the problem, it calls the AnnotationDriver inside a CachedDriver, my cache driver was without any implementation, so it didn't return anything and therefore the AnnotationDriver wasn't called.

What I did for the AnnotationDriver to be called was to recreate an implementation of the doctrine/cache ArrayDriver, for the development environment should be enough for now, then I think about another implementation with a better engine.

When I recreated the ArrayDriver implementation, my code started to recognize the entities correctly.

As mentioned in the issue below, I think it would be very useful to be able to disable the cache mechanism through the settings. https://github.com/Roave/psr-container-doctrine/issues/46