In Symfony2, when adding mapping information to tell Doctrine how to map my entity to the database, if I use YAML or XML format instead of PHP annotations, how/where do I write getters/setters/other functions?
1 Answers
you can define mapping in
yml
orxml
and still define entity class methods in the.php
Symfony Docs
Symfony docs show the metadata as annotations directly inside the Product class (option #1 PHP) at a different location src/AppBundle/Entity/Product.php
than alternative (option #2 YAML and #3 XML) src/AppBundle/Resources/config/doctrine/Product.orm.*ml
. This suggests that you can define the mapping in *ml
and the getters/setters/other functions in the php
.
Notice in the docs example the AppBundle\Entity\Product
is specified in both files. The class methods and mapping can be defined independently and related to each other as AppBundle\Entity\Product
.
Stackoverflow
Also note, this other question is misleading. You DO have to write getters/setters even if you use YAML/XML, as this answer clarifies:
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.