Question: Why is Doctrine\ORM\EntityRepository#getEntityManager() protected? #5635

Closed
opened 2026-01-22 15:13:23 +01:00 by admin · 1 comment
Owner

Originally created by @spackmat on GitHub (Aug 11, 2017).

Originally assigned to: @Ocramius on GitHub.

Hi,

I am refactoring my Symfony controllers to get the EntityRepositories injected instead of the whole EntityManager. This works fine, but in some cases (transactions, changing listeners etc.), I still need access to the EntityManager.

But the Doctrine\ORM\EntityRepository#getEntityManager() method is protected. This surely is intentional, so my question is: Would it be bad practice to access the EntityManager through (injected) repositories? Or are there other reasons for the protection?

Greets,
spackmat

Originally created by @spackmat on GitHub (Aug 11, 2017). Originally assigned to: @Ocramius on GitHub. Hi, I am refactoring my Symfony controllers to get the EntityRepositories injected instead of the whole EntityManager. This works fine, but in some cases (transactions, changing listeners etc.), I still need access to the EntityManager. But the Doctrine\ORM\EntityRepository#getEntityManager() method is protected. This surely is intentional, so my question is: Would it be bad practice to access the EntityManager through (injected) repositories? Or are there other reasons for the protection? Greets, spackmat
admin added the Question label 2026-01-22 15:13:23 +01:00
admin closed this issue 2026-01-22 15:13:24 +01:00
Author
Owner

@Ocramius commented on GitHub (Aug 11, 2017):

Would it be bad practice to access the EntityManager through (injected) repositories?

Yes, it is out of the responsibility of the EntityRepository to provide its dependencies to you. The same applies to basically any service. The method should be private, but it was made protected since this class was initially designed for inheritance.

You'd inject a separate EntityManager.

@Ocramius commented on GitHub (Aug 11, 2017): > Would it be bad practice to access the EntityManager through (injected) repositories? Yes, it is out of the responsibility of the `EntityRepository` to provide its dependencies to you. The same applies to basically any service. The method should be `private`, but it was made `protected` since this class was initially designed for inheritance. You'd inject a separate `EntityManager`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5635