6

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:

Uncaught exception 'Doctrine\Common\Persistence\Mapping\MappingException' with message 'The class 'Test\Bundle\UserBundle\Entity\User' was not found in the chain configured namespaces ' in...

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

http://localhost/

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

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

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']);
}

My Symfony Application is running on an IIS Webserver.

Do you guys know where I made a mistake?

Adrian
  • 807
  • 2
  • 12
  • 25
  • 2
    did you do a `php app/console cache:clear --env=prod` ? – KhorneHoly Aug 19 '15 at 13:31
  • I tried to do it but I get an error that I can't rename the cache directory "prod" – Adrian Aug 19 '15 at 13:45
  • Then go to the directory `app/cache/prod` and delete everything within this directory. – KhorneHoly Aug 19 '15 at 13:47
  • 2
    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 http://stackoverflow.com/questions/97875/rm-rf-equivalent-for-windows – tftd Aug 19 '15 at 13:49
  • Thanks I totally forgot about the processes. It worked now thank you very much! – Adrian Aug 19 '15 at 13:54

2 Answers2

5

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.

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

Custom Mapping Entities in a Bundle

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

Mapping Entities Outside of a Bundle

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

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

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

Community
  • 1
  • 1
tftd
  • 16,203
  • 11
  • 62
  • 106
  • Since my Entity is in the Entity directory and I use annotations I could use just the auto_mapping because my configuration is not somewhere else right? – Adrian Aug 19 '15 at 13:52
  • Yes, it should be working fine that way. I have my entities in `App/MyBundle/Entity/EntityName` and in the config I have `auto_mapping: true` - works like a charm. You still need to clear the cache after applying the change, though. – tftd Aug 19 '15 at 13:53
  • Yes I left it like this now and I cleared the cached now (after closing all php processes) It works like a charm. Could you add to your answer that you have to close all processes for clearing the cache of the production? Then I can accept your answer. – Adrian Aug 19 '15 at 13:56
0

It turns out that my mistake was that I forgot to add distant bundles/bundles in "vendor" inside my AppKernel file.

They weren't registered in the "registerBundles" function.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
aneth101
  • 509
  • 5
  • 9