mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
EntityRepository's private readonly properties make implementation overly opinionated #7482
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @oojacoboo on GitHub (Mar 5, 2025).
So, the
EntityRepositoryclass has all the properties marked asprivate readonly. The expectation here is that you're going to construct your repository class with these and they'd never need to be changed.Then comes the repository factory, which returns the repository for use with the
EntityManager.It seems the decision has been made that your repository classes should only be accessed through the
EntityManagerand that the constructor of your repositories shouldn't be available to pass additional services.If you attempt to instantiate your repositories with additional services (different per service as would be the logical case), say with a service container - that'll work fine, but you must then use that container within the factory to return your instantiated repositories.
The issue here is that you're now stuck with an
EntityManagerthat's pre-defined for that repository. You cannot pass or update theEntityManagerassociated with a repository through the factory. If you're using a customEntityManager, say with a specific connection, and you call$em->getRepository(Foo::class), you'll then end up with the pre-definedEntityManageron that repository, not the EntityManager you're currently callinggetRepositorywith. And, because everything isprivate readonlyon theEntityRepository, you cannot update it either.Also, the
RepositoryFactory::getRepositorymethod is typed to return theEntityRepositoryand not an interface. So you have to extend theEntityRepository. Otherwise, you could just wrap it and compose your own base class.