Symfony 4 was released on November 30th.
Update now to the best Symfony ever!

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 src/Mycompany/MyBundle/Resources/config/doctrine/Mycompany.MyBundle.Entity.Orderset.orm.yml instead of src/Mycompany/MyBundle/Resources/config/doctrine/metadata/orm/Mycompany.MyBundle.Entity.Orderset.dcm.yml.

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 metadata information.

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 in 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 src/Mycompany/MyBundle/Resources/config/doctrine/metadata/orm/Mycompany.MyBundle.Entity.Orderset.dcm.yml file and enjoy using app/config/mapping.orm.yml.

Feedback is the key to improve Symfony2.

UPDATE: As of Symfony2 beta2, mapping for a single entity must be done using the short name: src/Mycompany/MyBundle/Resources/config/doctrine/Orderset.orm.yml.

Comments

Nice!
If I do choose to go for a single file per entity, will my performance still benefit from the one file per entity rule?
Yes, performance will be better for single file. But only in the case where metadata is not in the cache.
">
Holy cow! Thank you, Fabien! I feel so... so... like the kid who asks Santa Claus for a pony and wakes up to actually find one!
Quick get a towel for nathan!...
One mapping to rule them all...
So are there 3 options to define schemas with doctrine, right?

1) src/Mycompany/MyBundle/Resources/config/doctrine/Mycompany.MyBundle.Entity.Orderset.orm.yml
2) src/Mycompany/MyBundle/Resources/config/doctrine/mapping.orm.yml
3) app/config/mapping.orm.yml

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?
Great simplification. I like! :-)
@Fabien Potencier. Will you make practical symfony2 documentation like jobeet? if will, when? good luck your job!!!
Up for #9tsendee - I'd love to see Jobbet-like tutorial for Symfony 2 :)
i found this page http://sftuts.com/doc/jobeet/en/index.html . i think, this documentation is great but not official. i need more practical tutorial of Symfony2. good luck Symfony2 core team............
Niceone boys
what about configure inactive/active bundles in external files? e.g: bundles.xml, bundles_dev.xml and bundles_test.xml
So if the global mapping requires a prefix, how would it work if I want to store definitions for more than one class in the global mapping.orm.xml? for instance, Blog and User?
Actually, after applying the fix at https://github.com/pkruithof/symfony/commit/e65585849847a48a972d784848e2d7720022ceef, it works with a more global prefix such as MyCompany or MyCompany/MyBundle.
Great job!
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.

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.