Raul Cejas
Raul Cejas

Reputation: 479

not returning correct EntityManager Symfony

I am implementing symfony security. My User entity is in another database. I have multiples enties managers. So far I can't get the entitymanager to read User table, which in my case is not the default entitymanager.

This is my security.yaml

security:
    encoders:
        App\Entity\User:
            id: 'App\Security\Encoder\MyCustomPasswordEncoder'
            #app_encoder:
            #para oracle
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\Trimu\User
                property: email
                manager_name: trimu
                #https://symfony.com/doc/4.0/security/entity_provider.html
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true
            lazy: true
            provider: app_user_provider
            guard:
                authenticators:
                    - App\Security\LoginFormAuthenticator
            logout:
                path: app_logout
                #target: app_logout
            remember_me:
                secret:   '%kernel.secret%'
                lifetime: 2592000 # 30 days in seconds

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        #- { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }

this is my doctrine.yaml

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                # configure these for your database server
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(resolve:DATABASE_URL)%'
            trimu:
                # configure these for your database server
                driver: 'oci8'
                server_version: ~
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(resolve:DATABASE_SECURITY_URL)%'
        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'
    orm:
        default_entity_manager: default
        #auto_generate_proxy_classes: true
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: 'App\Entity'
                        alias: App
            trimu:
                connection: trimu
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Trimu'
                        prefix: 'App\Entity\Trimu'
                        alias: App

As you see my entity App\Entity\Trimu\User is in other directory and prefix. I have read: Symfony 2.8 : Doctrine getManagerForClass() not returning the right Entity Manager and I have the same question: How does the getManagerForClass() method find out which entity manager is the right one for a specific class?

Thank you.

SOLVED, Thanks friends

I changed all prefix an all directory now it work fine.

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                # configure these for your database server
                driver: 'pdo_mysql'
                server_version: '5.7'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(resolve:DATABASE_URL)%'
            trimu:
                # configure these for your database server
                driver: 'oci8'
                server_version: ~
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                url: '%env(resolve:DATABASE_SECURITY_URL)%'
        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'
    orm:
        default_entity_manager: default
        #auto_generate_proxy_classes: true
        entity_managers:
            default:
                connection: default
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Mysql'
                        prefix: 'App\Entity\Mysql'
                        alias: App
            trimu:
                connection: trimu
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
                mappings:
                    App:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Trimu'
                        prefix: 'App\Entity\Trimu'
                        alias: App

As Stev say: Your first entity manager is covering all the entities because of :

dir: '%kernel.project_dir%/src/Entity'

Upvotes: 0

Views: 1216

Answers (1)

Stev
Stev

Reputation: 1112

Your first entity manager is covering all the entities because of :

dir: '%kernel.project_dir%/src/Entity'

Change your folder structure so that each EntityManager has it's own folder where to find the mappings. Example:

entity_managers:
        default:
            connection: default
            naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
            mappings:
                App:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/App'
                    prefix: 'App\Entity\App'
                    alias: App
        trimu:
            connection: trimu
            naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
            mappings:
                App:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Entity/Trimu'
                    prefix: 'App\Entity\Trimu'
                    alias: Trimu

Upvotes: 1

Related Questions