With it's usage of simple PHP objects for entities, Doctrine is a very easy-to-use ORM. It instantiates these entities using reflection. If you want to use entities that rely on another object via dependency injection, however, then you're stuck with setter injection.
This code allows you to pull new entities from a factory instead, allowing constructor injection and cleaner code.
How to use
First up, install this plugin.
composer require dittto/doctrine-entity-factories
Next, let's create a factory for our entity. These needs to implement EntityFactoryInterface.
If you're using multiple entity managers, this may change a bit, but you'll probably also have the understanding to make this work for your code.
Laravel, above, allows us to easily load additional code via it's service providers. For Symfony, we're going to take a slightly different approach and decorate Doctrine's EntityManager. The aim of both of these approaches is the same - to add our entity factories to it before Doctrine starts creating entities.
For Symfony, we're going to add some new services: