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

Closed
opened 2026-01-22 13:11:39 +01:00 by admin · 4 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:39 +01:00
admin closed this issue 2026-01-22 13:11:39 +01:00
Author
Owner

@doctrinebot commented on GitHub (Mar 28, 2011):

Comment created by @beberlei:

There is a third way, use an event listener and hook into "loadMetadata", which i recommend in your place.

@doctrinebot commented on GitHub (Mar 28, 2011): Comment created by @beberlei: There is a third way, use an event listener and hook into "loadMetadata", which i recommend in your place.
Author
Owner

@doctrinebot commented on GitHub (Mar 29, 2011):

Comment created by dmdeller:

OK, that works I guess. Thanks for the info.

My solution, in case it helps anyone else: https://gist.github.com/892404

@doctrinebot commented on GitHub (Mar 29, 2011): Comment created by dmdeller: OK, that works I guess. Thanks for the info. My solution, in case it helps anyone else: https://gist.github.com/892404
Author
Owner

@doctrinebot commented on GitHub (Mar 29, 2011):

Comment created by @beberlei:

Closed

@doctrinebot commented on GitHub (Mar 29, 2011): Comment created by @beberlei: Closed
Author
Owner

@doctrinebot commented on GitHub (Mar 29, 2011):

Issue was closed with resolution "Won't Fix"

@doctrinebot commented on GitHub (Mar 29, 2011): Issue was closed with resolution "Won't Fix"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1358