New alternative for getDoctrine() in Symfony 5.4 and up

New alternative for getDoctrine() in Symfony 5.4 and up

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The getDoctrine() Shortcut Is Dead: What Replaces It in Symfony 5.4+

If you are upgrading to Symfony 5.4 or later, you might notice your IDE flagging a familiar pattern as deprecated. Specifically, the AbstractController::getDoctrine() method is no longer recommended for accessing the Doctrine ORM manager directly from a controller. This change isn't always explicitly highlighted in high-level documentation or changelogs, which can lead to confusion when refactoring legacy codebases.

The core issue lies in how controllers should interact with the service container. The getDoctrine() shortcut was essentially a convenience method that pulled the default Doctrine Manager out of the kernel's service definition. While this worked perfectly fine in earlier versions, the Symfony team has moved away from injecting services directly into the controller class via static methods or magic accessors. Instead, the standard practice is to rely on constructor injection.

Why the Shortcut Disappeared

The deprecation isn't about Doctrine itself; it's about dependency injection principles. In modern Symfony architecture, controllers are expected to be thin wrappers that orchestrate logic rather than holding heavy infrastructure dependencies like database managers directly in their scope unless explicitly injected.

When you call $this->getDoctrine()->getManager(), you are bypassing the constructor. This makes testing difficult because you cannot easily swap the default Doctrine manager for a mock during unit tests without modifying the controller's internal state or relying on service locator patterns that defeat the purpose of DI. Furthermore, if your application has multiple database connections configured (e.g., read replicas, separate logging databases), the static shortcut forces you to always use the default connection, whereas constructor injection allows you to explicitly request a specific service alias defined in config/packages/doctrine.yaml.

The Modern Approach: Constructor Injection

The direct replacement for $this->getDoctrine()->getManager() is accessing the EntityManagerInterface instance that was injected into your controller's constructor.

Here is how you should refactor a typical controller:

// Before (Deprecated)
class ProductController extends AbstractController
{
    public function index(): Response
    {
        $em = $this->getDoctrine()->getManager();
        // ... logic using $em
    }
}

// After (Recommended)
use Doctrine\ORM\EntityManagerInterface;

class ProductController extends AbstractController
{
    private EntityManagerInterface $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function index(): Response
    {
        // Direct access without the shortcut
        $product = $this->entityManager->getRepository(Product::class)->find($id);
        
        return $this->render('product/show.html.twig', [
            'product' => $product,
        ]);
    }
}

In this example, the controller explicitly requests the EntityManagerInterface in its constructor. The Symfony kernel ensures that a valid instance is provided based on your configuration. This pattern aligns with how other services like Twig, Validator, or Messenger are handled in newer versions of the framework.

Handling Multiple Connections

One significant advantage of this approach appears when dealing with multiple database connections. If you have configured a specific connection alias for read-only operations in your doctrine.yaml, you can inject that specific service directly:

use Doctrine\ORM\EntityManagerInterface;

class ReportController extends AbstractController
{
    private EntityManagerInterface $readEntityManager;
    // ... other dependencies

    public function __construct(EntityManagerInterface $readEntityManager)
    {
        $this->readEntityManager = $readEntityManager;
    }
    
    // Injecting the specific connection alias from config
}

You can define this alias in your configuration file under doctrine.connections:

# config/packages/doctrine.yaml
doctrine:
    connections:
        default:
            url: '%env(DATABASE_URL)%'
        read_replica:
            url: '%env(DATABASE_REPLICA_URL)%'
            primary: false
    orm:
        auto_generate_proxy_classes: true
        entity_managers:
            default:
                connections:
                    default:
                        - ~ # uses the 'default' connection defined above
                    read_replica:
                        - ~

While this specific setup requires careful mapping in the EntityManager service definition, it demonstrates why the static shortcut is problematic: it hardcodes the dependency to a single source without giving you control over which instance your application logic actually needs.

Migration Path

If you encounter the deprecation warning during an upgrade, simply refactor the controller by adding the EntityManagerInterface as a constructor argument. If you were relying on getDoctrine() for complex repository queries or transaction management, ensure those are moved to the injected manager instance. This shift may require updating type hints and potentially restructuring how services are registered if you rely heavily on static accessors across your application layers.

Our network