Symfony – Should I use YAML/XML or annotation mapping in Doctrine 2

doctrine-ormsymfony

I use Doctrine 2 as ORM in Symfony framework. Using annotation-based mapping for entities, I would have to write lot of code (setters and getters, mapping information etc.). Using YAML/XML I won't have to write much just the column definition and metadata. In Doctrine documentation, they use mostly annotations in example with few using YAML or XML.

Should I use YAML/XML or annotations?

For annotations, I can find the documentation for every annotation at http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html. I couldn't find the equivalents of annotation in YAML or XML though; how do I convert the annotations to equivalent YAML/XML expressions?

Best Answer

This is basically down to preference.

This is my view of things:

Pros of annotations:

  • easier to work with, since they are close to what they describe (the properties)
  • lots of examples use them, as you have already noticed

Pros of yaml/xml

  • this keeps the domain objects clean, so absolutely nothing from the persistence layer leaks into the domain (even if in the form of a comment)

If you change the db or the domain, you still have to maintain them in either form, so there's no real advantage in any direction from that standpoint.

From a performance standpoint, in production you should be using doctrine's caching to cache the mappings, so they are equal here too.

Regarding this: "I would have to write lot of code, setters and getters, mapping information":

Doctrine requires private/protected properties, so you'll still be writing getters and setters. And you'll still be writing the mapping info, just in another place.

Personally, I'd go with annotations, since it's a lot easier to find examples and info if you need them.

Related Topic