16
votes

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

4
Inject Doctrine\ORM\EntityManagerInterface instead of ObjectManager.yivi
But on the doc, they inject ObjectManager : symfony.com/doc/current/service_container/parent_services.html , so is it really a problem ?eronn
Oh okay it's for the services. So, no the problem is solved for that. But it makes a new error on my repositories : Cannot autowire service "App\Repository\AbsenceRepository": argument "$registry" of method "__construct()" references interfac e "Symfony\Bridge\Doctrine\RegistryInterface" but no such service exists. Try changing the type-hint to "Doctrine\Common\Persi stence\ManagerRegistry" instead. While it's Doctrine that puts this class by default when it creates the repositorieseronn
So I do not know what version of Symfony I was. Probably 4.3.x, that's for sure. But yes I also thought that it came from an update of Doctrine. As a result, in my project, I had to replace all ObjectManagers with EntityManagerInterface, and all RegistryInterface with ManagerRegistry. And now it looks good, but it's still annoying that it does not happen alone, because if one day my site is in production and I do a compose update and there are changes like this, then the site will be HS the time I understand that I have to use other classeseronn
Doing a composer update in production can lead to all kinds of fun thing happening. Something still does not add up. The Doctrine tweaks happened several years ago. Maybe early in the Symfony 3 lifecycle when autowire was being introduced. Doing a composer update on an existing working 4.3 project would not cause these sorts of problems.Cerad

4 Answers

35
votes

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);
}
2
votes

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

1
votes

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

composer require doctrine/doctrine-bundle 1.12.2

0
votes

Change the Namespace of ObjectManager to Doctrine\Persistence\ObjectManager; instead of Doctrine\Common\Persistence\ObjectManager;