8

I use doctrine in my symfony 2.3 application. I want to use a folder structure like

/MyBundleName/User/User.php

for my Entities.

Question:

Is there anyway that I can explicitly map doctrine ORM directly to use an explicit directory instead of defaulting to the Entity Directory of my Bundle?

I would like to keep all related files in their respective directory such as ProductProvider in

/MyBundleName/Product/ProductProvider.php

Any help would be greatly appreciated.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Elijah McPherson
  • 81
  • 1
  • 1
  • 2
  • It is possible to tell doctrine explicite where your entities are. But I'm not sure if it's possible to point more than one location per one entity manager. If just one "custom" location per em is satisfiable for you then I'll post the answer – Cyprian Aug 29 '13 at 07:58

3 Answers3

14

Just to follow up a bit on @Imanol's correct answer, it is possible to have your entities in multiple directories under one entity manager:

doctrine:
  orm:
    default_entity_manager:       default
    auto_generate_proxy_classes: %kernel.debug%

    entity_managers:

        default:
            connection: default
            mappings:

        test01:
            connection: test01
            mappings:
              product:
                type:      yml
                dir:       %kernel.root_dir%/../src/Cerad/Bundle/Test01Bundle/Product
                prefix:    Cerad\Bundle\Test01Bundle\Product
                alias:     Product
                is_bundle: false
              user:
                type:      yml
                dir:       %kernel.root_dir%/../src/Cerad/Bundle/Test01Bundle/User
                prefix:    Cerad\Bundle\Test01Bundle\User
                alias:     User
                is_bundle: false

Don't worry about the is_bundle: false entries. The entities can still live in a bundle. Doctrine does not care. And in case you are wondering, the alias parameter lets you do things like:

$repo = $em->getRepository("Product:Product");
Cerad
  • 48,157
  • 8
  • 90
  • 92
  • Can you please fix the indentation of the code? Is `orm:` a node of `doctrine`? Is `test01:` a node of `mappings:`? Thanks. – A.L Jan 21 '16 at 22:18
  • orm is a node of doctrine. test01 is a node of entity_managers. http://symfony.com/doc/current/reference/configuration/doctrine.html – Cerad Jan 21 '16 at 23:53
4

you can tell Doctrine the directory where is your entities

doctrine:
orm:
    auto_generate_proxy_classes: %kernel.debug%
    auto_mapping: false
    mappings:
        name:
            type: php
            dir: %kernel.root_dir%/../src/Company/CartoDBBundle/Tests/CartoDB/Entity

Here you have the full documentation Doctrine configuration

I made a similar question a few days ago, there you can read the full answer Cedar gave me
Similar post

Community
  • 1
  • 1
Imanol
  • 250
  • 2
  • 7
0

I've spent some time trying to figure out the simplest case. This is how I made it work:

doctrine:
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true
        mappings:
            AppBundle:
                mapping: true
                type: annotation
                dir: Model
                alias: AppBundle
                prefix: 'AppBundle\Model'
                is_bundle: true

I simply wanted to store my entities in a directory called 'Model' inside my bundle, instead of the default 'Entity'.

Adrian
  • 1
  • 1