Skip to content
Advertisement

New alternative for getDoctrine() in Symfony 5.4 and up

As my IDE points out, the AbstractController::getDoctrine() method is now deprecated.

I haven’t found any reference for this deprecation neither in the official documentation nor in the Github changelog.

What is the new alternative or workaround for this shortcut?

Advertisement

Answer

As mentioned here:

Instead of using those shortcuts, inject the related services in the constructor or the controller methods.

You need to use dependency injection.

For a given controller, simply inject ManagerRegistry on the controller’s constructor.

use DoctrinePersistenceManagerRegistry;

class SomeController {

    public function __construct(private ManagerRegistry $doctrine) {}

    public function someAction(Request $request) {
        // access Doctrine
        $this->doctrine;
    }
} 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement