本文介绍了Symfony2-在链配置的名称空间中找不到类"X"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决我的问题的方法.但是我找不到任何解决方案.

I've been searching forever to solve my problem. But I can't find any solution.

当我尝试打开主页时,总是收到以下错误消息:

I always get this error message when I try to open the homepage:

在...的链配置名称空间中未找到消息为"Test \ Bundle \ UserBundle \ Entity \ User"类的未捕获异常"Doctrine \ Common \ Persistence \ Mapping \ MappingException"

奇怪的是,只有在具有以下URL时我才能得到它:

The weird thing is that I only get it when I have following URL:

http://localhost/

但是当我在此URL上运行它时,我没有收到任何错误,并且我的页面正在正确显示:

But when I run it on this URL I don't get any error and my page is being displayed correctly:

http://localhost/app_dev.php

我的配置"如下所示(config.yml):

My Configuration looks like this (config.yml):

# Doctrine Configuration
dbal:
  default_connection: default
  connections:
    default:
      driver:   "%database_driver%"
      host:     "%database_host%"
      port:     "%database_port%"
      dbname:   "%database_name%"
      user:     "%database_user%"
      password: "%database_password%"
      charset:  UTF8
    test:
      driver:   "%database_driver2%"
      host:     "%database_host2%"
      port:     "%database_port2%"
      dbname:   "%database_name2%"
      user:     "%database_user2%"
      password: "%database_password2%"
      charset:  UTF8
orm:
  default_entity_manager: default
  entity_managers:
    default:
      connection: default
      mappings:
        TestUserBundle:
          type: annotation

然后我在自定义服务中这样调用Doctrine:

And I call Doctrine in my custom service like this:

public function __construct(EntityManager $em)
{
    $repositiory = $em->getRepository('Test\Bundle\UserBundle\Entity\User');
    $this->user = $repositiory->find($_SERVER['AUTH_USER']);
}

我的Symfony应用程序正在IIS Web服务器上运行.

My Symfony Application is running on an IIS Webserver.

你们知道我在哪里犯错了吗?

Do you guys know where I made a mistake?

推荐答案

映射名称为TestUserBundle,但路径为Test\Bundle\User\Bundle,不是应该将其命名为TestBundleUserBundle吗?另外,通常在开发模式下将mappingsauto_generate_proxy_classes设置为true,这可能解释了为什么它在这里工作而不在产品中工作.

Mapping name is TestUserBundle but the path is Test\Bundle\User\Bundle, shouldn't it be named TestBundleUserBundle instead? Also, usually the mappings and auto_generate_proxy_classes are set to true in dev mode which might explain why it's working there and not in prod.

您可能想签出文档(Symfony 2.7),该文档显示了如何根据情况正确配置映射.

You might want to checkout the documentation (Symfony 2.7) which shows how exactly you should be configuring the mappings depending on your case.

doctrine:
    # ...
    orm:
        # ...
        auto_mapping: true
        mappings:
            # ...
            AppBundle:
                type: xml
                dir: SomeResources/config/doctrine

将实体映射到捆绑包之外

doctrine:
    # ...
    orm:
        # ...
        mappings:
            # ...
            SomeEntityNamespace:
                type: annotation
                dir: "%kernel.root_dir%/../src/Entity"
                is_bundle: false
                prefix: App\Entity
                alias: App

最后但并非最不重要的一点是,在对config.ymlapp/config/目录中的文件进行更改后,始终清除缓存目录.

Last, but not least, always clear your cache directory after applying changes to the config.yml or files in the app/config/ directory.

如评论中所述:您需要暂时停止可能使用prod目录的所有PHP进程(即,如果您已经运行console server:run),然后重试.如果这样做没有效果,请尝试

As mentioned in the comments:You need to temporarily stop any PHP processes that might be using the prod directory (i.e. if you've ran console server:run) and try again. If that doesn't do it, try this

这篇关于Symfony2-在链配置的名称空间中找不到类"X"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 13:58