{"record":{"id":"9f9312a63c2e56f2","repo":"doctrine/orm","slug":"no-second-level-cache-is-configured-on-the-given-e","errorCode":null,"errorMessage":"No second-level cache is configured on the given EntityManager.","messagePattern":"No second-level cache is configured on the given EntityManager\\.","errorType":"console","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Tools/Console/Command/ClearCache/CollectionRegionCommand.php","lineNumber":70,"sourceCode":"<info>%command.name% 'Entities\\MyEntity' 'collectionName' --flush</info>\n\nFinally, be aware that if <info>--flush</info> option is passed,\nnot all cache providers are able to flush entries, because of a limitation of its execution nature.\nEOT);\n    }\n\n    protected function execute(InputInterface $input, OutputInterface $output): int\n    {\n        $ui = (new SymfonyStyle($input, $output))->getErrorStyle();\n\n        $em         = $this->getEntityManager($input);\n        $ownerClass = $input->getArgument('owner-class');\n        $assoc      = $input->getArgument('association');\n        $ownerId    = $input->getArgument('owner-id');\n        $cache      = $em->getCache();\n\n        if (! $cache instanceof Cache) {\n            throw new InvalidArgumentException('No second-level cache is configured on the given EntityManager.');\n        }\n\n        if (( ! $ownerClass || ! $assoc) && ! $input->getOption('all')) {\n            throw new InvalidArgumentException('Missing arguments \"--owner-class\" \"--association\"');\n        }\n\n        if ($input->getOption('flush')) {\n            $cache->getCollectionCacheRegion($ownerClass, $assoc)\n                ->evictAll();\n\n            $ui->comment(\n                sprintf(\n                    'Flushing cache provider configured for <info>\"%s#%s\"</info>',\n                    $ownerClass,\n                    $assoc,\n                ),\n            );\n","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/doctrine/orm/blob/d9b9ff73016bf598ae07515f97289ce8074e97a5/src/Tools/Console/Command/ClearCache/CollectionRegionCommand.php#L52-L88","documentation":"The orm:clear-cache:region:collection console command evicts second-level-cache regions for a collection. It operates on $em->getCache(), which only returns a Cache instance when the second-level cache was enabled in the ORM configuration; otherwise it is null and the command throws InvalidArgumentException because there is nothing to clear.","triggerScenarios":"Running bin/console orm:clear-cache:region:collection (or doctrine:orm:clear-cache:region:collection via doctrine-bundle) on an EntityManager whose configuration never enabled the second-level cache (no second_level_cache block, no setSecondLevelCacheConfiguration).","commonSituations":"Symfony apps using doctrine-bundle without an orm.second_level_cache section; deploy scripts that run every cache command unconditionally; copied cache-clear snippets in projects that only use query/result caches.","solutions":["If you use the second-level cache, enable it in configuration (doctrine-bundle: orm.second_level_cache with a region cache driver) and re-run the command.","If you do not use it, drop the command from your deploy/CI scripts; nothing is cached to clear.","Guard scripted calls: run the command only when $em->getCache() instanceof Cache."],"exampleFix":"# before\n$ php bin/console doctrine:orm:clear-cache:region:collection \"App\\Entity\\User\" \"addresses\"\n# InvalidArgumentException: No second-level cache is configured...\n\n# after (if SLCache is wanted) enable it, e.g. doctrine-bundle config:\n# doctrine:\n#     orm:\n#         second_level_cache:\n#             region_cache_driver: { type: file_system }","handlingStrategy":"type-guard","validationCode":"use Doctrine\\ORM\\Cache;\nuse Doctrine\\ORM\\EntityManager;\n\nfunction slcConfigured(EntityManager $em): bool\n{\n    return $em->getCache() instanceof Cache;\n}\n\n// before running the command programmatically:\nif (slcConfigured($em)) {\n    // safe to run orm:clear-cache:region:collection\n}","typeGuard":"use Doctrine\\ORM\\Cache;\nuse Doctrine\\ORM\\EntityManagerInterface;\n\n/** @psalm-assert Cache $cache */\nfunction assertSlcCache(EntityManagerInterface $em): void\n{\n    $cache = $em->getCache();\n    assert($cache instanceof Cache);\n}","tryCatchPattern":"When invoking the console command from a script, catch \\InvalidArgumentException and degrade to a notice instead of failing the deploy: catch (\\InvalidArgumentException $e) { $io->note('Second-level cache not configured, skipping'); return 0; }","preventionTips":["Condition cache-clear steps on the feature being enabled, not on habit.","Assert getCache() instanceof Cache in an environment smoke test where SLCache is expected.","Keep one source of truth for EntityManager configuration so CLI and web see the same caches."],"tags":["doctrine-orm","second-level-cache","console","cache","symfony","cli"],"backgroundTag":"missing-cache-configuration","analyzedSha":"d9b9ff73016bf598ae07515f97289ce8074e97a5","analyzedAt":"2026-08-21T06:13:15.863Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}