1

I am building a company Symfony2 library that includes several bundles and a library of classes that can be used by any code. The plan is to eventually turn this into an internal package and install it into projects via composer so that it will live in the vendor directory.

One component of the library has a pair of database entity classes and a single repository class associated with it.

Because the only logic associated with these classes is contained in the repository class, it doesn't seem to make sense to to surround it with a bundle.

I have, as yet, been unable to get Doctrine to load this repository. It initially failed with a

The class 'Dplh\Library\DplhEnum\Entity\EnumGroupRepository' was not found in the chain configured namespaces Dplh\DplhSecurityBundle\Entity

exception. (The reference to my security bundle was initially rather confusing. Turns out that this is Doctrine helping out by listing all of the known entity namespaces (see Symfony error The class XXX was not found in the chain configured namespaces XXX)).

As I understand it, this happens because Doctrine is configured to use auto-mapping and expects all entities to be defined in src/WhateverBundle/Entity directories.

This can be changed with additional Doctrine configuration for specific mappings in config.yml (http://zalas.eu/how-to-store-doctrine-entities-outside-of-a-symfony-bundle/).

So far, I have been unable to get this to work either. I expect it's a minor config thing somewhere. I added the following to config.yml:

doctrine:
    dbal:
      # ...
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

        mappings:
            DplhEnum:
                type:       annotation
                is_bundle:  false
                dir:        %kernel.root_dir%/../src/Dplh/Library/DplhEnum/Entity
                prefix:     Dplh\Library\DplhEnum\Entity
                alias:      DplhEnum

I get two different exceptions depending on my call to getRepository().

// Using the fully-qualified class name.
$this->getEntityManager()->getRepository('Dplh\Library\DplhEnum\Entity\EnumGroup' )

This throws

Attempted to load class "EnumGroupRepository" from namespace "Dplh\DplhEnumBundle\Entity". Did you forget a "use" statement for another namespace?

// Using the alias.
$this->getEntityManager()->getRepository('DplhEnum\EnumGroup' )    

this throws

Class 'DplhEnum\EnumGroup' does not exist

I have verified that the EnumGroupRepository.php file is in the src/Dplh/Library/DplhEnum/Entity directory and the EnumGroupRepository class is in the Dplh\Library\DplhEnum\Entity namespace and that they are in the correct directory.

Community
  • 1
  • 1
David Patterson
  • 1,780
  • 3
  • 17
  • 40
  • I think you need to use a colon for namespaces getRepository('DplhEnum:EnumGroup') – Cerad Jul 10 '15 at 19:22
  • Well. That's interesting. Using 'DplhEnum:DplhEnum' throws "Attempted to load class "EnumGroupRepository" from namespace "Dplh\DplhEnumBundle\Entity". Did you forget a "use" statement for another namespace?". Note the DplhEnumBundle reference. Odd. – David Patterson Jul 10 '15 at 19:39
  • Using `getRepository('DplhEnum:EnumGroup')` should work. Is your entity defined under the `Dplh\Library\DplhEnum\Entity` namespace? – Javier Eguiluz Jul 11 '15 at 07:42
  • Yes, it is. And it's in the src/Dplh/Library/DplhEnum/Entity directory. – David Patterson Jul 11 '15 at 15:47

0 Answers0