Symfony 错误 The class XXX was not found in the chain configured namespaces XXX

2022-01-16 00:00:00 php symfony doctrine

已经有一些关于这个主题的其他问题,但没有一个是真正有帮助的.我是 Symfony 的新手,所以很难理解它.

There are some other questions on this subject already, but none of them were really helpful. I am new to Symfony, so it's pretty hard to get my head around it.

我在文件 ClientIntranetBundleLDAPLDAPAuthenticationProvider.php 中,此代码导致错误:

I am in the file ClientIntranetBundleLDAPLDAPAuthenticationProvider.php and this code is causing an error:

$user = new LDAPUser($username);

我确实添加了它的命名空间:

I did add it's namespace which is:

use ClientIntranetBundleLDAPLDAPUser;

LDAPUser 实现 UserInterface

LDAPUser implements UserInterface

我得到的错误是

The class 'ClientIntranetBundleLDAPLDAPUser' was not found in the chain
configured namespaces ClientClientBundleEntity

这是什么意思?根据我的阅读,它与映射有关.

What is that suppose to mean? From what I read it has something to do with the mapping.

我在 config.yml 中的 Doctrine orm 设置为:

My Doctrine orm in the config.yml is set to:

 orm:
    auto_generate_proxy_classes: %kernel.debug%
    auto_mapping: true

希望你能帮助我.

编辑#1:

其实我发现不是

$user = new LDAPUser($username);

这是导致错误的原因,但它是在我尝试保留此实体时:

That is causing the error, but it is when I am trying to persist this entity:

$entityManager->persist($user);

编辑 #2:

我对映射出了什么问题感到困惑:

I'm confused with what's wrong with the mapping:

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="ClientIntranetBundleLDAPLDAPUser" table="users" repository-class="ClientClientBundleRepositoryUserRepository">
    <id name="id" type="integer" column="id">
        <generator strategy="AUTO" />
    </id>
    <field name="username" column="username" type="string" length="100" />
</entity>

也许是因为我在两个捆绑包之间跳跃?

Maybe it's because I'm jumping between two bundles?

推荐答案

默认情况下,auto_mapping 功能会在 Entity 命名空间下查找实体,因此给定您的实体不存在,Doctrine 对此一无所知.

By default, the auto_mapping feature looks for entities under the Entity namespace, so given that your entity is not there, Doctrine does not know anything about it.

您需要将您的实体放在 Entity 命名空间下或手动配置 Doctrine 以添加您的自定义实体命名空间.这样您就失去了 auto_mapping 功能,因此您需要手动注册每个捆绑包:

You need to put your entity under the Entity namespace or configure Doctrine by hand to add your custom entity namespace. This way you lose the auto_mapping feature, so you would need to register every bundle manually:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    entity_managers:
        default:
            mappings:
                MyBundle:
                    type: annotation
                custom_mapping:
                    type: annotation
                    prefix: ClientIntranetBundleLDAP
                    dir: "%kernel.root_dir%/src/Client/IntranetBundle/LDAP/"
                    is_bundle: false

如您所见,最好将所有内容放在您的包中的 Entity 命名空间下,让 Doctrine 来完成这项艰巨的工作.

As you can see, it's better to put everything under the Entity namespace in your bundle and let Doctrine do the hard work.

相关文章