4

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

Klyner
  • 3,983
  • 4
  • 26
  • 50
  • 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 Jul 31 '20 at 08:23
  • So you suggest to remove every file within /var/cache, right? And after that run `composer install` or something like that? – Klyner Jul 31 '20 at 08:34
  • 1
    If you're not in production I suggest to remove files in the /var/cache and then launch the application (with**out** 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 Jul 31 '20 at 08:39
  • 1
    @Klyner I am also facing the same issue. Got any solutions? I have posted a similar issue in https://stackoverflow.com/questions/63219927/symfony-5-1-3-issue-while-clearing-the-cache-attempted-to-load-class-mappingdr – Nandakumar V Aug 03 '20 at 07:02
  • 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 Aug 03 '20 at 12:13
  • @Klyner I have found a solution and have updated my SO https://stackoverflow.com/questions/63219927/symfony-5-1-3-issue-while-clearing-the-cache-attempted-to-load-class-mappingdr/63235538#63235538 – Nandakumar V Aug 04 '20 at 04:35

1 Answers1

2

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.

Alexandre Tranchant
  • 4,426
  • 4
  • 43
  • 70
  • 1
    I have tried both solutions but I get the same errors as mentioned above. For solution 2 only step 1 applied to me, so I only updated the recipes. – Klyner Jul 31 '20 at 09:02
  • Thanks for your feedback. 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 ManagerRegistry. Do not hesitate to edit your question when you find where you're using Registry. – Alexandre Tranchant Jul 31 '20 at 09:13
  • Thank you for your help! I am trying to find where in my code I use Registry but searching in the entire project with `Registry` does not result in a file that is found. Even when I look for Doctrine, this is not found in any php file. Does this mean I can maybe even remove the import? (the `doctrine.yaml` and `doctrine_migrations.yaml` and from `bundles.php`) Or is this not possible because of the error? – Klyner Jul 31 '20 at 09:25
  • If your application is storing some data in a database and use an EntityManager, you shall not remove these package, else you can try. These packages aren't listed in your composer.json. So,another package needs them. You can find which one by looking at your composer.lock file. – Alexandre Tranchant Jul 31 '20 at 09:42
  • @AlexandreTranchant Does it solve the `Attempted to load class "MappingDriverChain" from namespace ` issue? I have tried updating all packages, still the issue is not solved. Any idea where is it happening? – Nandakumar V Aug 03 '20 at 09:59