Home > Enterprise >  New alternative for getDoctrine() in Symfony 5.4 and up
New alternative for getDoctrine() in Symfony 5.4 and up

Time:12-03

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?

CodePudding user response:

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.

class SomeController {

    public function __construct(private ManagerRegistry $doctrine) {}

    public function someAction(Request $request) {
        // access Doctrine
        $this->doctrine;
    }
} 
  • Related