Symfony2: Getting Easier (part 3)
In a previous blog post about "Getting easier", Nathan made this useful comment:
**Great stuff**! I applaud this direction. Is there any chance the location for Doctrine schema files could be shortened? I made files like this for each entity: `src/Mycompany/MyBundle/Resources/config/doctrine/metadata/orm/Mycompany.MyBundle.Entity.Orderset.dcm.yml` But I'm like, "are you serious? What happened to `config/schema.yml`?" I like Larry Wall's philosophy about Perl - "make easy things easy and hard things possible".
We have heard you Nathan! In the upcoming beta2, we have shortened the path by
removing the unneeded
metadata/orm/ part. So, defining the mapping for your
Orderset class can now be done in
That's slightly better but not that much, isn't it? Unfortunately, Doctrine2 enforces the one file per class rule, and forces the file to be named after the fully qualified class name. But as I used to be a Perl developer, and because shorter file names are always better, I've tried to find some ways to get around the rule.
It turned out to be quite easy and DoctrineBundle now supports the definition
of several mapping metadata into one single file. So, you can also define your
Orderset class mapping data in
src/Mycompany/MyBundle/Resources/config/doctrine/mapping.orm.yml. This is
even shorter than before and of course totally optional. You can mix and match
both possibilities into a single bundle: define most of your mapping data into
one main file, and still use individual files for entities that have many
But what about the possibility to define all your mapping data into a central
location? If you are like me, you want to be able to reuse your Model outside
of a Symfony2 context; and so you don't want to store your Entity classes and
their mapping data under a specific bundle. So, what about storing everything
app/config/mapping.orm.yml for instance? Well, that's also possible and
here is a simple working configuration:
doctrine: orm: mappings: global: type: yml dir: %kernel.root_dir%/config prefix: Blog\Entity
The good news is that the
doctrine:generate:entities command is also able to
deal with it natively:
./app/console doctrine:generate:entities Blog/Entity --path=src/
Say good bye to your old
file and enjoy using
Feedback is the key to improve Symfony2.
UPDATE: As of Symfony2 beta2, mapping for a single entity must be done using the short name:
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
If I do choose to go for a single file per entity, will my performance still benefit from the one file per entity rule?
I really like the idea to make Symfony2 easy (to use), although i think when there are so many options it's difficult to follow a convention. What about the community Bundles? Is it right that everyone use distinct methods for defining configurations?
Got one small issue, when doing reverse engineer from existing database using "app/console doctrine:mapping:convert --from-database yml [target-path]", it still produces dcm.yml instead of orm.yml and all the entity name produced doesn't have any namespace.