Can I organize Doctrine entities or their mapping files into subfolders?
doctrine-orm, symfony, yaml
Solution
Long story short, this is possible.
If you have folder structure inside your bundle's Entity folder, it's simple. You have to name your ORM files using entity namespace part below of Entity namespace and replacing `\` with `.`.
So if, for example, you have `Project\TestBundle\Entity\Place\Type` entity, the ORM file (located in `config/doctrine` folder inside bundle) will have name `Place.Type.orm.yml`.
If you want to use as Doctrine entities classes from outside of Entity folder (or even outside of the bundle folder), it gets a little bit complicated, but still possible. Doctrine Bundle allows to define custom mapping locations for your classes in its configuration.
Again - example. If you have your entities inside of `Project\Test` namespace (in folder `src/Project/Test`), you can define mapping like this:
app/config/config*.yml
doctrine:
orm:
MyCustomDomain:
mapping: true
type: yml
dir: %kernel.root_dir%/config/projecttest
alias: ProjectTest
prefix: Project\Test
is_bundle: false
In fact, Doctrine Bundle does something similar automatically, that's why you can put all of your classes in Entity subfolder and fear no more.
The prefix is namespace prefix. Folder is the path to folder with configuration files. Alias is interesting - it allows to use simpler object names in DQL queries and mapping files. Symfony's `TestBundle:Test` syntax works on the same premise - `TestBundle`is the alias for all entities in TestBundle. `is_bundle` tells Doctrine, that the entities are outside of Symfony bundle and require a little bit different treatment.
There are some caveats in defining your own mapping. Mapper works using 'first match' rule on prefix. So if you declare your mapping on too broad namespace prefix, it can override other mappings.
Nevertheless, it is useful sometimes. For example, if you want to map classes from "foreign" library directly to Doctrine. Or are creating a library not completely tied to Symfony and want to keep some of your classes outside of the bundle.
Problem
I have tried this directory structure for my mapping files: ``` /config/doctrine/Place.orm.yml /config/doctrine/Place/Type.orm.yml /config/doctrine/Place/Price.orm.yml ``` And have pointed to the corresponding Entity in my mapping file like this: ``` Project\TestBundle\Entity\Place\Type: type: entity table: place_type id: id: type: integer generator: { strategy:AUTO } fields: name: type: string length: 255 ``` But this returns an error. The system can't seem to detect the mapping file for Entity.