Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 366
sonam Avatar asked Sep 21 '12 09:09

sonam


1 Answers

There is one disadvantage about using Annotations. Since they are defined on the comments (which is weird), you will NOT be able to use precompilers (like Zend Guard) or some bytecode php caching extensions to enhance your code performance (some of them strip the comments). Because annotation is the only specification with this technical impairment, I would NOT recommend using it.

Also, writing 'functional' stuff on comments is just weird.

XML is much more verbose than YAML, but is also more widely known. Documentation examples for XML is the poorest amongst all the options.

YAML is more readable than any of the others, but it also relies on space identation (which some people don't like). Symfony uses YAML by default for its configurations, so many people that use Symfony opt to use YAML for the doctrine mappings -- which means YAML examples are well documented and there are lots of resources out there.

I personally prefer YAML. I can generate the entities automatically using the doctrine schema tools (meaning I don't have to write the getters/setters). I can also use the precompilers/bytecode cache options out there without worrying about having my comments stripped out.


Additional information:

If you're using PHP 7.0 or higher, you can't ever set opcache.save_comments = 0 on php.ini if you're using annotations. PHP has a native way to strip comments, which WILL BREAK your application. Guilherme Blanco, one of Doctrine's most active maintainers and the guy who developed the annotation retrieval classes that are used by most projects (Doctrine, Symfony) has spoken out AGAINST comment annotations (if you read the entire thread, you'll see he's very passionate about this). There has also been some extensive discussion on the issues with comment annotations on Reddit, and most people seemed to agree it's a bad idea.

Despite all of that, Symfony's official best practices book advices people to use Annotations. So, most Symfony bundles will probably use annotations as their metadata format.

like image 163
Pedro Cordeiro Avatar answered Oct 11 '22 01:10

Pedro Cordeiro