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.
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.
If you want to organize your doctrine Entities into subfolders
for example: src/AppBundle/Entity/subfolder/MyEntity.php
then the corresponding ORM file should look like:
src/Resources/config/Doctrine/subfolder.MyEntity.orm.yml
Don't make subfolders inside src/Resources/config/Doctrine/
*
*Note: If you have a look at the Doctrine config docs it looks like it may be possible to configure a different location for your orm.yml file using:
doctrine:
orm:
# An array of mappings, which may be a bundle name or something else
mapping_name:
mapping: true
type: ~
dir: ~
alias: ~
prefix: ~
is_bundle: ~
but I have not tried this. maybe someone else can confirm to improve this answer?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With