Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 - no such service exists for ObjectManager after composer update

in my project symfony 4 I wanted to make a Composer update, something he did.

But since, it puts me an error on all my controllers when I use the ObjectManager in my constructors, like this :

use Doctrine\Common\Persistence\ObjectManager;

/**
     * Manager
     *
     * @var ObjectManager
     */
    private $manager;

public function __construct(ObjectManager $manager)
    {
        $this->manager = $manager;
    }

I've this kind of error :

Cannot autowire service "App\Controller\OrdreMissionController": argument "$manager" of method "__construct()" references interface "Doctrine\Common\Persistence\ObjectManager" but no such service exists. You should maybe alias this interface to the existing "doctrine.orm.default_entity_manager" service.

It applies to all my controllers since they all have the ObjectManager, I do not understand what is happening

like image 882
eronn Avatar asked Nov 20 '19 12:11

eronn


3 Answers

This seems to be due to the upgrade of doctrine-bundle => v2.0.0.

You have to change :

  • Symfony\Bridge\Doctrine\RegistryInterface => Doctrine\Common\Persistence\ManagerRegistry
  • Doctrine\Common\Persistence\ObjectManager => Doctrine\ORM\EntityManagerInterface

In your "App\Repository\AbsenceRepository" please update your constructor:

public function __construct(\Doctrine\Common\Persistence\ManagerRegistry $registry)
{
    parent::__construct($registry, Address::class);
}
like image 189
Laurent Rossillol Avatar answered Nov 19 '22 23:11

Laurent Rossillol


Similar issue to update Symfony 4.2 to 4.4,
In practice, I search/replace in all my src/Repository/*
Symfony\Bridge\Doctrine\RegistryInterface -> Doctrine\Common\Persistence\ManagerRegistry in USE
and
RegistryInterface -> \Doctrine\Common\Persistence\ManagerRegistry in __construct

if you use vim (vi -p src/Repository/*.php), here are:

:tabdo %s/Symfony\\Bridge\\Doctrine\\RegistryInterface/Doctrine\\Common\\Persistence\\ManagerRegistry/cg
:tabdo %s/RegistryInterface/\\Doctrine\\Common\\Persistence\\ManagerRegistry/cg

and my site works fine in 4.4.8

like image 35
bcag2 Avatar answered Nov 20 '22 00:11

bcag2


If not you can also come back to version 1.12.2 of doctrine-bundle

composer require doctrine/doctrine-bundle 1.12.2

like image 1
Papa Matrood Avatar answered Nov 20 '22 01:11

Papa Matrood