DDC-1085: [PATCH] Allow setting custom default classes for EntityManager and EntityRepository #1356

Open
opened 2026-01-22 13:11:36 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Mar 28, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user dmdeller:

Recently, I wanted to create some common functionality for my project's EntityRepositories. So I created a custom EntityRepository class. However, I needed to make this the repository for all (or most) of my Entities.

I had two options:

  1. Add the repositoryClass attribute to the @Entity annotation of every single Entity class.
  2. Extend EntityManager to hand out my custom EntityRepository, instead of the Doctrine one, whenever it's not overridden by an annotation.

Option 1 was not desirable because it's a lot of copy/pasted code and it's easy to forget when creating a new Entity.

Option 2 was not desirable because the logic in EntityManager#getRepository() could not be easily called from a child class, so I would need to copy/paste the implementation, making my subclass brittle against upstream changes. I further discovered that EntityManager::create() has the same problem.

To make this easier for projects with a similar need, I created a patch for Doctrine which would allow specifying the class name of the EntityManager and EntityRepository that Doctrine will use by default in these two methods. Please consider merging, or let me know what changes would be necessary for the patch to be accepted.

Pull request: https://github.com/doctrine/doctrine2/pull/41

Originally created by @doctrinebot on GitHub (Mar 28, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user dmdeller: Recently, I wanted to create some common functionality for my project's EntityRepositories. So I created a custom EntityRepository class. However, I needed to make this the repository for all (or most) of my Entities. I had two options: 1. Add the repositoryClass attribute to the @Entity annotation of every single Entity class. 2. Extend EntityManager to hand out my custom EntityRepository, instead of the Doctrine one, whenever it's not overridden by an annotation. Option 1 was not desirable because it's a lot of copy/pasted code and it's easy to forget when creating a new Entity. Option 2 was not desirable because the logic in EntityManager#getRepository() could not be easily called from a child class, so I would need to copy/paste the implementation, making my subclass brittle against upstream changes. I further discovered that EntityManager::create() has the same problem. To make this easier for projects with a similar need, I created a patch for Doctrine which would allow specifying the class name of the EntityManager and EntityRepository that Doctrine will use by default in these two methods. Please consider merging, or let me know what changes would be necessary for the patch to be accepted. Pull request: https://github.com/doctrine/doctrine2/pull/41
admin added the Improvement label 2026-01-22 13:11:36 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1356