没有为 class App\Entity\Category 定义实体管理器

No entity manager defined for class App\Entity\Category

我遵循了 symfony (https://symfony.com/doc/master/bundles/SonataAdminBundle/getting_started/creating_an_admin.html) 的入门教程,并且一切正常(Category 和 BlogPost)

之后我做了一些修改来处理 SonataUserBundle 并让这个后端只为管理员保护。

我以某种方式做到了这一点,只有在以管理员角色登录时我才能访问管理员。

但现在当我尝试继续分类或 BlogPost 时,出现错误:

No entity manager defined for class App\Entity\Category

No entity manager defined for class App\Entity\BlogPost

我可能在安装用户管理时搞砸了一些东西,但我看不到它在哪里。

services.yaml

admin.category:
    class: App\Admin\CategoryAdmin
    arguments: [~, App\Entity\Category, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, label: Category }
admin.blog_post:
    class: App\Admin\BlogPostAdmin
    arguments: [~, App\Entity\BlogPost, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, label: 'Blog Post' }

sonata_admin.yaml

sonata_admin:
    title: 'Sonata Admin'
    dashboard:
        blocks:
            - { type: sonata.admin.block.admin_list, position: left }


    security:
        handler: sonata.admin.security.handler.role

        role_admin: ROLE_ADMIN
        role_super_admin: ROLE_SUPER_ADMIN


sonata_block:
    blocks:
        sonata.admin.block.admin_list:
            contexts: [admin]

security.yaml

security:
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers

    firewalls:
        # Disabling the security for the web debug toolbar, the profiler and Assetic.
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # -> custom firewall for the admin area of the URL
        admin:
            pattern:            /admin(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
                target:         /admin/login
            anonymous:          true

        # -> end custom configuration

        # default login area for standard users

        # This firewall is used to handle the public login area
        # This part is handled by the FOS User Bundle
        main:
            pattern:             .*
            context:             user
            form_login:
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
            logout:             true
            anonymous:          true

            # 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 }
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

    role_hierarchy:
        # for convenience, I decided to gather Sonata roles here
        ROLE_SONATA_FOO_READER:
            - ROLE_SONATA_ADMIN_DEMO_FOO_LIST
            - ROLE_SONATA_ADMIN_DEMO_FOO_VIEW
        ROLE_SONATA_FOO_EDITOR:
            - ROLE_SONATA_ADMIN_DEMO_FOO_CREATE
            - ROLE_SONATA_ADMIN_DEMO_FOO_EDIT
        ROLE_SONATA_FOO_ADMIN:
            - ROLE_SONATA_ADMIN_DEMO_FOO_DELETE
            - ROLE_SONATA_ADMIN_DEMO_FOO_EXPORT

        # those are the roles I will use (less verbose)
        ROLE_STAFF:             [ROLE_USER, ROLE_SONATA_FOO_READER]
        ROLE_ADMIN:             [ROLE_STAFF, ROLE_SONATA_FOO_EDITOR, ROLE_SONATA_FOO_ADMIN]
        ROLE_SUPER_ADMIN:       [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

        # you could alternatively use for an admin who has all rights
        ROLE_ALL_ADMIN:         [ROLE_STAFF, ROLE_SONATA_FOO_ALL]

        # set access_strategy to unanimous, else you may have unexpected behaviors
        access_decision_manager:
            strategy: unanimous

    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

如果您需要更安静的代码,请告诉我,我看不到也不明白哪里出了问题。

编辑:添加 doctrine.yaml

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci
        types:
            json: Sonata\Doctrine\Types\JsonType

        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: true
        entity_managers:
            default:
                mappings:
                    SonataUserBundle: ~
                    FOSUserBundle: ~
                    ApplicationSonataUserBundle: ~

您似乎需要为 App

添加映射
# config/packages/doctrine.yaml
doctrine:
    ...
    orm:
        ...
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App