3
votes

I want to decouple my application so I can use my entities anywhere whatever their datasource might be.

So I've put my entities in

DataAccessLayerBundle\Entity

and mappings in

DataProvider\DataBaseBundle\Resources\config\doctrine

Issue:

When I try the following command:

php app\console doctrine:schema:create

to build my database, I get the following error:

Warning: class_parents(): Class (..)\DataProvider\DatabaseBundle\Entity\BaseEntity does not exist and could not be loaded in (...)\vendor\doctrine\lib\D octrine\ORM\Mapping\ClassMetadataFactory.php line 223

The question is: why the hell is it trying to look for classes in the DataProvider\DatabaseBundle\Entity namespace, when I explicitly defined it otherwise in the mapping files? Example fragment of mapping file:

<mapped-superclass name="(...)\DataAccessLayerBundle\Entity\Base\BaseEntity">

I've noticed symfony/doctrine is just looking up those classes based off xml filenames (e.g. BaseEntity.orm.xml) and current bundle namespace.

Is there any way I can decouple things like I wanted, or am I stuck with defining entities and their mapping in the same bundle?

1
you said you put your entities in DataAccessLayerBundle\Entity but your mapping is telling it to look in DataAccessLayerBundle\Entity\Base\BaseEntity... ?JamesHalsall
yeah that's just one case, the rest of entities rest in DataAccessLayerBundle\Entity\ - that's not the caseItako

1 Answers

4
votes

As it turned out from the docs, it is possible to have entities and mappings in separate bundles. To do so, you have to configure doctrine in the main config as in following example:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    ...
    entity_managers:
      default:
        mappings:
          (...)DataAccessLayerBundle:
            type: xml
            dir: ../DataProvider/DatabaseBundle/Resources/config/doctrine

I still had to move my BaseEntity class from Entity\Base to Entity folder for this to work, but this is a little sacrifice compared to the flexibility gained.

Config reference: http://symfony.com/doc/2.0/reference/configuration/doctrine.html