31

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.

I am in the file Client\IntranetBundle\LDAP\LDAPAuthenticationProvider.php and this code is causing an error:

$user = new LDAPUser($username);

I did add it's namespace which is:

use Client\IntranetBundle\LDAP\LDAPUser;

LDAPUser implements 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:

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

Hopefully you can help me.

EDIT #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:

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?

3 Answers3

46

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.

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: 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.

Alberto Fernández
  • 1,568
  • 13
  • 22
  • I see! But why is it looking in the Entity namespace of another bundle? Right now LDAPUser is in bundle Client\IntranetBundle, but the error says: was not found in the chain configured namespaces Client\ClientBundle\Entity. –  Apr 02 '14 at 14:24
  • 2
    When an exception like that is thrown, Doctrine will dump all the configures namespaces, basically as a debugging tool. As you have only one bundle registered, you only have one entity namespace configured. You should see more once you have more bundles registered, given that you are presented again with the same exception. – Alberto Fernández Apr 02 '14 at 14:27
  • No, I have two bundles that are registered in the AppKernel :/. I'm still confused why it's looking in the Entity folder of another bundle. –  Apr 02 '14 at 14:31
  • Ah but you are right that I don't have any Entity folder in the IntranetBundle. –  Apr 02 '14 at 14:33
  • 1
    Do you have entities in the other bundle? If you don't, Doctrine of course is not going to add to the configured namespaces a namespace that does not exists. – Alberto Fernández Apr 02 '14 at 14:33
  • I was wondering why `php app/console doctrine:schema:update` wasn't picking up anything and running `php app/console cache:clear` gave me this type of error. Moving things to the `...\Entity` namespace fixed the issue. – jxmallett May 21 '15 at 00:22
  • A lifesaver. Thank you so much. – Anjana Silva Jun 27 '20 at 14:49
2

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

They weren't registered in the registerBundles() method.

yivi
  • 42,438
  • 18
  • 116
  • 138
aneth101
  • 509
  • 5
  • 9
1

Your bundle should be mapped with correct entity managers you are using for service / command / api in config/packages/doctrine.yaml (Symfony4) config file.

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.

orm:
    entity_managers:
        default:
            mappings:
              BundleName:
Hemant Thorat
  • 2,386
  • 20
  • 14