Base entities and behaviors for Doctrine — how I do it.
If you work with Symfony, then you probably work with Doctrine as well. How do you create new entities? I try to make it easy.
I made a package Base Entity Bundle and I am actively using it in my personal / hobby projects.
Why I created it?
Base fields for entities
Always have to define the base fields (id, createdAt, updatedAt, etc.) for every new entity. Why should I repeat the same mandatory code parts if I could solve it easier?
Doctrine behaviors
In the last few years, I had problems with the maintenance of the existing doctrine behavior packages. First of all, I appreciate the developer’s work on these packages, I have used these for years.
My problems with the existing packages:
- StofDoctrineExtensionsBundle: I used it for years but in somewhere 2019 had a maintenance problem. So after that, I switched my projects to an alternative. After a while, the package was maintained again, and as I see still maintained correctly. Symfony 7 wasn’t supported until the end of June 2024.
- DoctrineBehaviors: I used this package alternatively, but after a while, this hadn’t been maintained anymore. They are still searching for a maintainer as I know, and some forks also exist.
It had worked correctly for me until I wanted to use Symfony 7.
I could use a fork from these packages or migrate to the currently maintained one, but for me, this is too much effort. I don’t want to wait or migrate again if I can do it myself.
Conclusion and solution
Originally, I created this package to provide base entities for projects.
With this package, you can easily make entities without any extra effort. It supports int, uuid, and ulid ids for Doctrine entities:
class <your-entity-name> extends IdBasedEntity {}
class <your-entity-name> extends UuidBasedEntity {}
class <your-entity-name> extends UlidBasedEntity {}
After the base conception, I decided that I wanted to create a doctrine behavior package that implements the behaviors that I commonly used in my projects.
The supported behaviors now are Timestampable, SoftDeleteable, Blameable
(The Doctrine behavior implementations are not 100% equivalent to the original DoctrineBehaviors package.)
For more details check the GitHub repo.
Summary
If you like my package, please share this article with your friends and colleagues.
Do you have some ideas or feedback? Give me your opinions, here or in the GitHub repo.
If you want to contribute to the package, then also get in touch with me with one of the options above.