Best-practice for Entities using ORM-Annotations or loadMetadata()

I’ve a question again where I could not figure out the “best-practice” or current state-of-the-art.

For writing entity classes, some entities in Mautic core using @ORM annotations, while other entities using the method loadMetadata(ORM\ClassMetadata $metadata) to define the ORM mappings.

Other entities even using a mix of both, like here:

What is the clean or future-proof way here? Doing both (Annotation + loadMetadata()) seems to be double-maintenance work, and IMO it should be the one or the other (except there is a reason like legacy functionality or so) :thinking:

Do you have a link to a Mautic entity that is using ORM annotations? I’m not aware of any.

Isn’t the mentioned CitrixEvent entity from core, because it ships from the core-repository?

My question rised as we took over some plugins from another agency which ONLY used ORM annotations and left out the loadMetadata() method in their entities. Because these plugins was “recently created”, I thought this might be the proper way, and the current core entities coming from “older days” (haven’t been refactored for legacy reasons).

Once never know… But from your answer I could interprete that annotations are the wrong way and loadMetadata() should be used?!

The CitrixEvent has some annotations. Not sure why as it also has the loadMetadata() method with the same info:

The decision to go with loadMetadata over annotations was made due to massive amount of cache files it generated which cause troubles when hosting several hundreds of Mautic instances. Did you know that a file system has a limit of amount of files it can hold? So that limit was reached and the solution at the time was to get rid of yaml files, twig and annotations and so the cache size decreased considerably. That’s just the history behind the decision.

Now we continue to do it this way to keep consistency. We are forced to refactor PHP templates with Twig in Mautic 5 as PHP template support was dropped in Symfony 5. That was massive undertaking. On the other hand, the Symfony community seem to be going from yaml configs towards PHP configs. It’s interesting to look back at some decisions made almost a decade ago and see how it turned out.

2 Likes

Thank you very much for this explanation, that makes a lot of sense! Personally I prefer the PHP configuration as well, it feels more intuitive (less “magic” behind the scenes).

What you mentioned about Symfony is going (back) towards PHP configs sounds a bit hilarious to me, because I remember the early days of PHP development where we had only PHP configs. Then they started with YAML which was as dev somehow annoying from the very beginnin (because of caching etc), and now they move back to PHP … :sweat_smile: Do you have any source about this, or it is more a subjective feeling?

Tomas Votruba is major voice behind the movement :slight_smile: 10 Cool Features You Get after switching from YAML to PHP Configs | Tomas Votruba

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.