How to solve Symfony 5 Exception: Argument #1 ($registry) must be of type Doctrine\Common\Persistence\ManagerRegistry, Doctrine\Bundle\DoctrineBundle\Registry given

How to solve Symfony 5 Exception: Argument #1 ($registry) must be of type Doctrine\Common\Persistence\ManagerRegistry, Doctrine\Bundle\DoctrineBundle\Registry given

As always, upgrading stuff aggressively on my project messes stuff up. In this case, an old custom repository class that was created when my project was using Symfony 4 and that no one used until now, triggered the mentioned exception when importing it inside a controller:

App\Repository\UserRepository::__construct(): Argument #1 ($registry) must be of type Doctrine\Common\Persistence\ManagerRegistry, Doctrine\Bundle\DoctrineBundle\Registry given

The class that is triggering the exception right now in my Symfony 5 project is the following one:

<?php

namespace App\Repository;

use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\Query;

/**
 * @method User|null find($id, $lockMode = null, $lockVersion = null)
 * @method User|null findOneBy(array $criteria, array $orderBy = null)
 * @method User[]    findAll()
 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class UserRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, User::class);
    }
}

To solve this exception, you need to replace the current ManagerRegistry that is being used from the Doctrine/Common namespace to the Doctrine/Persistence one. Replace:

use Doctrine\Common\Persistence\ManagerRegistry;

With this:

use Doctrine\Persistence\ManagerRegistry;

After replacing the used class, the problem should be now gone. In my case, the class now looks like this:

<?php

namespace App\Repository;

use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
// use Doctrine\Common\Persistence\ManagerRegistry;
// Note: use the ManagerRegistry class from the Doctrine\Persistence namespace
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\Query;

/**
 * @method User|null find($id, $lockMode = null, $lockVersion = null)
 * @method User|null findOneBy(array $criteria, array $orderBy = null)
 * @method User[]    findAll()
 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class UserRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, User::class);
    }
}

Happy coding ❤️!

This could interest you

Become a more social person