Search code examples
symfonyormdoctrine

symfony doctrine orm mappings wildcard


Symfony 6.3

I want to place entities in multiple subfolders, for now i have a config like this:

doctrine:
orm:
    auto_generate_proxy_classes: '%kernel.debug%'
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true
    mappings:
        App\Core:
            is_bundle: false
            type: attribute
            dir: '%kernel.project_dir%/src/Core/Entity'
            prefix: 'App\Core\Entity'
            alias: AppCore
        App\Shared:
            is_bundle: false
            type: attribute
            dir: '%kernel.project_dir%/src/Shared/Entity'
            prefix: 'App\Shared\Entity'
            alias: AppShared
        App\Chat:
            is_bundle: false
            type: attribute
            dir: '%kernel.project_dir%/src/Chat/Entity'
            prefix: 'App\Chat\Entity'
            alias: AppChat
        App\User:
            is_bundle: false
            type: attribute
            dir: '%kernel.project_dir%/src/User/Entity'
            prefix: 'App\User\Entity'
            alias: AppUser

Now, if i adding a new "module" with entities in subfolder, i should add a new config.

Is there any way to apply wildcard to this config? Something like this:

doctrine:
orm:
    auto_generate_proxy_classes: '%kernel.debug%'
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true
    mappings:
        App:
            is_bundle: false
            type: attribute
            dir: '%kernel.project_dir%/src/*/Entity'
            prefix: 'App\Entity'
            alias: App

I try to do so, but it not working.


Solution

  • You could try the following config. I'm not sure if it works, I haven't tested it.

    mappings:
        App:
            is_bundle: false
            type: attribute
            dir: '%kernel.project_dir%/src/'
            prefix: 'App'
            alias: App
    

    I'm not sure what else would happen to your other classes and the Symfony DI Container internally.

    Also don't forget to disable the autoloader for all those locations using the exclude list in your services.yaml file:

    App\:
       exclude:
         - '../src/*/Entity'   // not sure if you can use `**` as glob pattern here