Learn how to solve the Symfony 4 or 5 Uncaught Error: Argument 1 passed to App\Repository\Repository::__construct() must be an instance of Doctrine\Common\Persistence\ManagerRegistry.

Dealing with "old projects" is very usual and you will always find yourself in problems trying to set It up, but it's quite unusual to install minor updates of libraries on your Symfony project, to find out that someway, it broke and doesn't work anymore. This week, I simply deployed a project that is already running on a production server and decided to run a live copy of it, for my surprise, after configuring everything to make it work locally, the following exception showed up in all of my custom repository classes:

Uncaught Error: Argument 1 passed to App\Repository\MyRepository::__construct() must be an instance of Doctrine\Common\Persistence\ManagerRegistry, instance of Doctrine\Bundle\DoctrineBundle\Registry given

In this article, I'm going to explain to you how to solve this problem easily.

What causes the exception

In my case, I simply installed the dependencies of a project using composer install:

Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "5.1.*"
Package operations: 1 install, 83 updates, 0 removals
  - Updating doctrine/doctrine-bundle (2.1.0 => 2.1.2): Loading from cache
  - Updating doctrine/common (2.13.3 => 3.0.2): Loading from cache
Writing lock file
Generating optimized autoload files
composer/package-versions-deprecated: Generating version class...
composer/package-versions-deprecated: ...done generating version class
86 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Executing script cache:clear [OK]
Executing script assets:install public [OK]

As you can see, the package doctrine/common got updated from the 2.13.3 version to the 3.0.2 (a major update, so yeah, something could break), which means that if some of your code uses something that depends on this package, you will face this issue.

Triggering the exception

In the project, the following code would trigger the exception. This custom repository class for the Articles entity, relies on the Doctrine\Common\Persistence\ManagerRegistry class:

<?php

namespace App\Repository;

use App\Entity\Articles;
use App\Entity\Gallery;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
 * @method Articles|null find($id, $lockMode = null, $lockVersion = null)
 * @method Articles|null findOneBy(array $criteria, array $orderBy = null)
 * @method Articles[]    findAll()
 * @method Articles[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ArticlesRepository extends ServiceEntityRepository
{

    public function __construct(ManagerRegistry $registry, SessionInterface $session)
    {
        $this->session = $session;
        parent::__construct($registry, Articles::class);
    }

    // Rest of the code in the repository
}

Solution

To fix this problem, you only need to replace the use statement of the ManagerRegistry class. Instead of the following line, using the Doctrine/Common package:

use Doctrine\Common\Persistence\ManagerRegistry;

Use the class directly from the Doctrine package:

use Doctrine\Persistence\ManagerRegistry;

This should immediately fix your problem, don't forget to do this in all of your custom repository classes.

Happy coding ❤️!


Sponsors