4
votes

I am developing a Symfony project which somebody else started on and I was happy that I had it up and running. Unfortunatelly after running composer update today, the Symfony page that I have throws an error.

After running composer update the following error was displayed:

Composer update error

Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 255
!!  Symfony\Component\ErrorHandler\Error\ClassNotFoundError {#132
!!    #message: """
!!      Attempted to load class "MappingDriverChain" from namespace "Doctrine\Common\Persistence\Mapping\Driver".\n
!!      Did you forget a "use" statement for "Doctrine\Persistence\Mapping\Driver\MappingDriverChain"?
!!      """
!!    #code: 0
!!    #file: "./var/cache/dev/ContainerFcUQG2T/App_KernelDevDebugContainer.php"
!!    #line: 869
!!    trace: {
!!      ./var/cache/dev/ContainerFcUQG2T/App_KernelDevDebugContainer.php:869 {
!!        ContainerFcUQG2T\App_KernelDevDebugContainer->getDoctrine_Orm_DefaultEntityManagerService($lazyLoad = true)
!!        ›
!!        › $b = new \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain();
!!        › $b->addDriver(new \Doctrine\ORM\Mapping\Driver\AnnotationDriver(($this->privates['annotations.cached_reader'] ?? $this->getAnnotations_CachedReaderService()), [0 => (\dirname(__DIR__, 4).'/src/Entity')]), 'App\\Entity');
!!      }
!!      ./var/cache/dev/ContainerFcUQG2T/App_KernelDevDebugContainer.php:5160 { …}
!!      ./var/cache/dev/ContainerFcUQG2T/App_KernelDevDebugContainer.php:5204 { …}
!!      ./var/cache/dev/ContainerFcUQG2T/App_KernelDevDebugContainer.php:569 { …}
!!      ./vendor/symfony/http-kernel/CacheWarmer/CacheWarmerAggregate.php:87 { …}
!!      ./vendor/symfony/http-kernel/Kernel.php:553 { …}
!!      ./vendor/symfony/http-kernel/Kernel.php:126 { …}
!!      ./vendor/symfony/framework-bundle/Console/Application.php:168 { …}
!!      ./vendor/symfony/framework-bundle/Console/Application.php:74 { …}
!!      ./vendor/symfony/console/Application.php:140 { …}
!!      ./bin/console:42 { …}
!!    }
!!  }
!!  2020-07-31T08:18:05+00:00 [critical] Uncaught Error: Class 'Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain' not found
!!

After accessing the Symfony page, this TypeError was printed:

TypeError

Argument 1 passed to Doctrine\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector::__construct() must be an instance of Doctrine\Common\Persistence\ManagerRegistry, instance of Doctrine\Bundle\DoctrineBundle\Registry given, called in /var/www/html/mgo/var/cache/dev/ContainerFcUQG2T/App_KernelDevDebugContainer.php on line 1176

To be honest I have no idea what caused this issue, but I think it has nothing to do with the changes I made in the twig and Symfony php files of the project.

I'll add some files, if there is anything else that I should be looking into, let me know.

/config/packages/doctrine.yaml

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '5.7'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

composer.json

{
  "name": "project",
  "description": "project description",
  "type": "path",
  "license": "MIT",
  "minimum-stability": "dev",
  "require": {
    "php": "^7.4",
    "ext-ctype": "*",
    "ext-iconv": "*",
    "mgo/mgo-client-php": "2.0.0",
    "sensio/framework-extra-bundle": "^5.1",
    "symfony/asset": "5.0.*",
    "symfony/console": "5.0.*",
    "symfony/dotenv": "5.0.*",
    "symfony/expression-language": "5.0.*",
    "symfony/flex": "^1.3.1",
    "symfony/form": "5.0.*",
    "symfony/framework-bundle": "5.0.*",
    "symfony/http-client": "5.0.*",
    "symfony/intl": "5.0.*",
    "symfony/mailer": "5.0.*",
    "symfony/monolog-bundle": "^3.1",
    "symfony/notifier": "5.0.*",
    "symfony/orm-pack": "*",
    "symfony/process": "5.0.*",
    "symfony/security-bundle": "5.0.*",
    "symfony/serializer-pack": "*",
    "symfony/string": "5.0.*",
    "symfony/translation": "5.0.*",
    "symfony/twig-pack": "*",
    "symfony/validator": "5.0.*",
    "symfony/web-link": "5.0.*",
    "symfony/yaml": "5.0.*",
    "ext-json": "*"
  },
  "repositories": [
      {
      "type": "path",
      "url": "/home/*user*/mgo-client-php",
      "symlink": false
  }
  ],
  "require-dev": {
      "symfony/debug-pack": "*",
      "symfony/maker-bundle": "^1.0",
      "symfony/profiler-pack": "*",
      "symfony/test-pack": "*",
      "mgo-client-php": "2.0.0",
      "monolog/monolog": "^1.23"
  },
  "config": {
      "preferred-install": {
          "*": "dist"
      },
      "sort-packages": true
  },
  "autoload": {
      "psr-4": {
          "App\\": "src/"
      }
  },
  "autoload-dev": {
      "psr-4": {
          "App\\Tests\\": "tests/"
      }
  },
  "replace": {
      "paragonie/random_compat": "2.*",
      "symfony/polyfill-ctype": "*",
      "symfony/polyfill-iconv": "*",
      "symfony/polyfill-php72": "*",
      "symfony/polyfill-php71": "*",
      "symfony/polyfill-php70": "*",
      "symfony/polyfill-php56": "*"
  },
  "scripts": {
      "auto-scripts": {
          "cache:clear": "symfony-cmd",
          "assets:install %PUBLIC_DIR%": "symfony-cmd"
      },
      "post-install-cmd": [
          "@auto-scripts"
      ],
      "post-update-cmd": [
          "@auto-scripts"
      ]
  },
  "conflict": {
      "symfony/symfony": "*"
  },
  "extra": {
      "symfony": {
          "allow-contrib": false,
          "require": "5.0.*"
      }
  }
}

EDIT:

As @Alexandre Tranchant suggested I removed my /var/cache file to get the original error. The error that pops up is the following:

Attempted to load class "MappingDriverChain" from namespace "Doctrine\Common\Persistence\Mapping\Driver". Did you forget a "use" statement for "Doctrine\Persistence\Mapping\Driver\MappingDriverChain"?

enter image description here

1
I guess that composer upgrade your doctrine package version, could you manually clear the cache by deletint its content manually and eventually update the error message. - Alexandre Tranchant
So you suggest to remove every file within /var/cache, right? And after that run composer install or something like that? - Klyner
If you're not in production I suggest to remove files in the /var/cache and then launch the application (without relaunching composer install) to see the original error message. (But I encountered this bug and I posted an answer. Hope it helps you!) - Alexandre Tranchant
@Klyner I am also facing the same issue. Got any solutions? I have posted a similar issue in stackoverflow.com/questions/63219927/… - Nandakumar V
Thank you for mentioning. Currently unfortunatelly I have not managed to fix this yet. I have put back an older version (fortunatelly I had a backup) and updated that one. I try to not use composer update again because of this issue. Have you fixed it already maybe using one of the solutions below? - Klyner

1 Answers

2
votes

It's correct there is no link with your update on twig files.

As your composer.json file doesn't fix the doctrine version, it upgraded your doctrine package version. In the newest version of Doctrine, the pattern of repository files have change. There is 2 solutions:

Solution1 (Not tested and not recommended): you fix the doctrine package by adding it in the composer.json. You can have a look on the old composer.lock of your application. This isn't the best solution. (But it's only my opinion)

Solution2: Upgrade your files to be compliant with the new version of doctrine.

I see three major steps:

  1. Update the configuration file by updating recipes.
  2. Update the declaration of repositories (see below)
  3. Update fixtures files. (but I don't see fixtures in your composer files)

So, I guess that you have to upgrade your repositories declaration.

Here is the new pattern of a repository

#src/Repository/Foo.php
use App\Entity\Foo;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ManagerRegistry;


/**
 * Some repository coded in the new way.
 *
 * @method Article|null find($id, $lockMode = null, $lockVersion = null)
 * @method Article|null findOneBy(array $criteria, array $orderBy = null)
 * @method Article[]    findAll()
 * @method Article[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ArticleRepository extends ServiceEntityRepository
{
    /**
     * FooRepository constructor.
     *
     * @param ManagerRegistry $registry injected by dependency injection
     */
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Foo::class);
    }

If you don't use repositories, you have to find where in your code you was using Doctrine\Bundle\DoctrineBundle\Registry and find a way to replace it by the new ManagerRegistry.