php Symfony 错误在链配置的命名空间 XXX 中找不到类 XXX
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22813300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony error The class XXX was not found in the chain configured namespaces XXX
提问by Alberto Fernández
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.
关于这个主题已经有一些其他问题,但没有一个是真正有帮助的。我是 Symfony 的新手,所以很难理解它。
I am in the file Client\IntranetBundle\LDAP\LDAPAuthenticationProvider.php and this code is causing an error:
我在文件 Client\IntranetBundle\LDAP\LDAPAuthenticationProvider.php 中,此代码导致错误:
$user = new LDAPUser($username);
I did add it's namespace which is:
我确实添加了它的命名空间,即:
use Client\IntranetBundle\LDAP\LDAPUser;
LDAPUser implements UserInterface
LDAPUser 实现 UserInterface
The error I get is
我得到的错误是
The class 'Client\IntranetBundle\LDAP\LDAPUser' was not found in the chain
configured namespaces Client\ClientBundle\Entity
What is that suppose to mean? From what I read it has something to do with the mapping.
那是什么意思?从我读到的内容来看,它与映射有关。
My Doctrine orm in the config.yml is set to:
我在 config.yml 中的 Doctrine orm 设置为:
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
Hopefully you can help me.
希望你能帮助我。
EDIT #1:
编辑#1:
Actually, I found out that it was not
其实我发现不是
$user = new LDAPUser($username);
That is causing the error, but it is when I am trying to persist this entity:
那是导致错误的原因,但是当我尝试保留此实体时:
$entityManager->persist($user);
EDIT #2:
编辑#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="Client\IntranetBundle\LDAP\LDAPUser" table="users" repository-class="Client\ClientBundle\Repository\UserRepository">
<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?
也许是因为我在两个包之间跳跃?
回答by Alberto Fernández
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.
默认情况下,该auto_mapping
功能会在Entity
命名空间下查找实体,因此鉴于您的实体不存在,Doctrine 对此一无所知。
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:
您需要将实体置于Entity
命名空间下或手动配置 Doctrine 以添加自定义实体命名空间。这样你就失去了这个auto_mapping
功能,所以你需要手动注册每个包:
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
mappings:
MyBundle:
type: annotation
custom_mapping:
type: annotation
prefix: Client\IntranetBundle\LDAP\
dir: "%kernel.root_dir%/src/Client/IntranetBundle/LDAP/"
is_bundle: false
As you can see, it's better to put everything under the Entity
namespace in your bundle and let Doctrine do the hard work.
如您所见,最好将所有内容都放在Entity
包中的命名空间下,并让 Doctrine 完成繁重的工作。
回答by Hemant Thorat
Your bundle should be mapped with correct entity managers you are using for service / command / api in config/packages/doctrine.yaml
(Symfony4) config file.
您的包应该与您在config/packages/doctrine.yaml
(Symfony4) 配置文件中用于服务/命令/api 的正确实体管理器进行映射。
For example In following doctrine config, BundleName
is mapped with default
entity managers so code using default
entity managers doctrine connection can access and use BundleName
's entities and repositories.
例如,在以下原则配置中,BundleName
与default
实体管理器映射,因此使用default
实体管理器原则连接的代码可以访问和使用BundleName
的实体和存储库。
orm:
entity_managers:
default:
mappings:
BundleName:
回答by aneth101
My mistake was that I forgot to add distant bundles/bundles in "vendor" inside my AppKernel
file.
我的错误是我忘记在我的AppKernel
文件中的“供应商”中添加远程捆绑包/捆绑包。
They weren't registered in the registerBundles()
method.
他们没有在registerBundles()
方法中注册。