Symfony-docs: [Doctrine][4.4] Documentation not clear about how to use "metadata_cache_driver" (May result in Unknown cache of type "array" configured for cache "metadata_cache" in entity manager "default".)

Created on 9 Dec 2019  路  4Comments  路  Source: symfony/symfony-docs

On the Doctrine configuration page (https://symfony.com/doc/4.4/reference/configuration/doctrine.html) it states to use the following configuration to setup Doctrine:

doctrine:
    orm:
        auto_mapping: true
        # the standard distribution overrides this to be true in debug, false otherwise
        auto_generate_proxy_classes: false
        proxy_namespace: Proxies
        proxy_dir: '%kernel.cache_dir%/doctrine/orm/Proxies'
        default_entity_manager: default
        metadata_cache_driver: array
        query_cache_driver: array
        result_cache_driver: array

But when using it, i get the following error:

Unknown cache of type "array" configured for cache "metadata_cache" in entity manager "default".

Here is my configuration:

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_520_ci

        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_mapping: true
        # the standard distribution overrides this to be true in debug, false otherwise
        auto_generate_proxy_classes: false
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        metadata_cache_driver: array
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/App/Entity'
                prefix: 'App\Entity'
                alias: App

About my setup:

  • PHP 7.3
  • Symfony 4.4.1
  • Doctrine 2.7.0
  • Symfony ORM Pack 1.0.7

I also tried #10797, but it didn't help. Using metadata_cache_driver in doctrine.orm results in:

Symfony\Component\Config\Definition\Exception\InvalidConfigurationException: Unrecognized options "metadata_cache_driver, query_cache_driver, result_cache_driver" under "doctrine.orm". Available options are "auto_generate_proxy_classes", "default_entity_manager", "entity_managers", "proxy_dir", "proxy_namespace", "resolve_target_entities".


Did i missed something in the documentation?


EDIT: When running in dev environment, i had no problems, but after i started with PHPUnit tests requiring Doctrine, i ran into the error above.

Most helpful comment

Possible solution?

As described here, one just has to install

composer require symfony/proxy-manager-bridge

to solve the problem. Dont forget to clear your cache folder before trying it out!

As mentioned before, with dev environment i had no problems, they started arising when running test environment with PHPUnit bridge.

It worked for me together with the following changes:

config/doctrine.yaml

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        url: '%env(resolve:DATABASE_URL)%'
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_520_ci

    orm:
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_generate_proxy_classes: false
        metadata_cache_driver: #                <=== new
            type: service
            id: doctrine.system_cache_provider
        query_cache_driver: #                <=== new
            type: service
            id: doctrine.system_cache_provider
        result_cache_driver: #                <=== new
            type: service
            id: doctrine.result_cache_provider
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/App/Entity'
                prefix: 'App\Entity'
                alias: App

config/services.yaml

services:
    # ...
    doctrine.result_cache_provider: #        <==== new
        class: Symfony\Component\Cache\DoctrineProvider
        public: false
        arguments:
            - '@doctrine.result_cache_pool'
    doctrine.system_cache_provider: #        <==== new
        class: Symfony\Component\Cache\DoctrineProvider
        public: false
        arguments:
            - '@doctrine.system_cache_pool'

config/framework.yaml

framework:
    cache:
        # ....
        pools: #        <==== new
            doctrine.result_cache_pool:
                adapter: cache.app
            doctrine.system_cache_pool:
                adapter: cache.system

Can someone tell me, why i have to install symfony/proxy-manager-bridge here?

Shouldn't these information be added to the documentation? What do you think?

All 4 comments

If i understand the related DoctrineExtension class right, it seems that you can't use an array:

<?php
// ...
// around line 723

    switch ($cacheDriver['type'] ?? 'pool') {
        case 'service':
            $serviceId = $cacheDriver['id'];
            break;
        case 'pool':
            $serviceId = $this->createPoolCacheDefinition($container, $cacheDriver['pool'] ?? $this->createArrayAdapterCachePool($container, $objectManagerName, $cacheName));
            break;
        case 'provider':
            $serviceId = sprintf('doctrine_cache.providers.%s', $cacheDriver['cache_provider']);
            break;
        default:
            throw new \InvalidArgumentException(sprintf(
                    'Unknown cache of type "%s" configured for cache "%s" in entity manager "%s".',
                    $cacheDriver['type'],
                    $cacheName,
                    $objectManagerName
            ));
    }
?>

Source: https://github.com/doctrine/DoctrineBundle/blob/2.0.x/DependencyInjection/DoctrineExtension.php#L716

Possible solution?

As described here, one just has to install

composer require symfony/proxy-manager-bridge

to solve the problem. Dont forget to clear your cache folder before trying it out!

As mentioned before, with dev environment i had no problems, they started arising when running test environment with PHPUnit bridge.

It worked for me together with the following changes:

config/doctrine.yaml

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        url: '%env(resolve:DATABASE_URL)%'
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_520_ci

    orm:
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_generate_proxy_classes: false
        metadata_cache_driver: #                <=== new
            type: service
            id: doctrine.system_cache_provider
        query_cache_driver: #                <=== new
            type: service
            id: doctrine.system_cache_provider
        result_cache_driver: #                <=== new
            type: service
            id: doctrine.result_cache_provider
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/App/Entity'
                prefix: 'App\Entity'
                alias: App

config/services.yaml

services:
    # ...
    doctrine.result_cache_provider: #        <==== new
        class: Symfony\Component\Cache\DoctrineProvider
        public: false
        arguments:
            - '@doctrine.result_cache_pool'
    doctrine.system_cache_provider: #        <==== new
        class: Symfony\Component\Cache\DoctrineProvider
        public: false
        arguments:
            - '@doctrine.system_cache_pool'

config/framework.yaml

framework:
    cache:
        # ....
        pools: #        <==== new
            doctrine.result_cache_pool:
                adapter: cache.app
            doctrine.system_cache_pool:
                adapter: cache.system

Can someone tell me, why i have to install symfony/proxy-manager-bridge here?

Shouldn't these information be added to the documentation? What do you think?

Any feedback on this please?

I understand the docs in such a way that your solution of defining a service per cache pool is equivalent to configuring

orm:
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_generate_proxy_classes: false
        metadata_cache_driver: #                <=== new
            type: pool     <=== changed
            id: doctrine.system_cache_pool<=== changed
        query_cache_driver: #                <=== new
            type: pool
            id: doctrine.system_cache_pool
        result_cache_driver: #                <=== new
            type: pool
            id: doctrine.result_cache_pool

and omitting the service definition.
But:

  • Your solution (with service definition) works, but some apis do not function correctly, e.g. bin/console doctrine:cache:clear-metadata has no effect.
  • The solution without service just doesn't work for me, effectively I think only an array cache is in use (the snippet you posted above also indicates that in the else-part)
Was this page helpful?
0 / 5 - 0 ratings