Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 285
S. Dre Avatar asked Dec 05 '25 13:12

S. Dre


1 Answers

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 Doctrine\Persistence\ManagerRegistry;

class SomeController {

    public function __construct(private ManagerRegistry $doctrine) {}

    public function someAction(Request $request) {
        // access Doctrine
        $this->doctrine;
    }
} 
like image 117
yivi Avatar answered Dec 08 '25 03:12

yivi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!