The class «Entity» was not found in the chain configured namespaces in production mode #6716

Closed
opened 2026-01-22 15:37:25 +01:00 by admin · 0 comments
Owner

Originally created by @marcelkorpel on GitHub (May 12, 2021).

For a project I use separate databases and entity managers using Symfony 5.2.7 and Doctrine ORM 2.8.2. My doctrine.yaml looks like

doctrine:
    dbal:
        connections:
            myproject:
                driver: 'pdo_mysql'
                charset: utf8mb4
                default_table_options:
                    charset: utf8mb4
                    collate: utf8mb4_unicode_ci
                override_url: true
                url: '%env(resolve:DATABASE_URL_MYPROJECT)%'
    orm:
        auto_generate_proxy_classes: true
        entity_managers:
            myproject:
                connection: myproject
                mappings:
                    MyProject:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/MyProject'
                        prefix: 'App\Entity\MyProject'
                        alias: MyProject

(only one database and entity manager is currently used, but this is to be extended in the future).

When I try to get the repository class of an entity within a controller using

        $repository = $this->getDoctrine()->getRepository(Entity::class);

everything is fine, regardless of in which mode Symfony runs.

However, when trying to get that class from an autowired entity manager within a generic (service) class, like

class Service
{
    private ObjectRepository $repository;

    public function __construct(EntityManagerInterface $em)
    {
        $this->repository = $em->getRepository(Entity::class);
    }
    /* … */
}

Symfony outputs a 500 error when running in production mode and writes

request.CRITICAL: Uncaught PHP Exception Doctrine\Persistence\Mapping\MappingException: "The class 'App\Entity\MyProject\Entity' was not found in the chain configured namespaces " at /srv/http/myproject/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php line 23 {"exception":"[object] (Doctrine\\Persistence\\Mapping\\MappingException(code: 0): The class 'App\\Entity\\MyProject\\Entity' was not found in the chain configured namespaces  at /srv/http/myproject/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:23)"}

to /srv/http/myproject/var/log/prod.log.

For the record, the service class is also autowired in the controller method:

class TheController extends AbstractController
{
    /* … */
    public function router(string $path, Service $service): Response
    /* … */
}

Everything works as expected, no error is thrown or output in dev.log, when APP_ENV=dev.

Originally created by @marcelkorpel on GitHub (May 12, 2021). For a project I use separate databases and entity managers using Symfony 5.2.7 and Doctrine ORM 2.8.2. My `doctrine.yaml` looks like ```yaml doctrine: dbal: connections: myproject: driver: 'pdo_mysql' charset: utf8mb4 default_table_options: charset: utf8mb4 collate: utf8mb4_unicode_ci override_url: true url: '%env(resolve:DATABASE_URL_MYPROJECT)%' orm: auto_generate_proxy_classes: true entity_managers: myproject: connection: myproject mappings: MyProject: is_bundle: false type: annotation dir: '%kernel.project_dir%/src/Entity/MyProject' prefix: 'App\Entity\MyProject' alias: MyProject ``` (only one database and entity manager is currently used, but this is to be extended in the future). When I try to get the repository class of an entity within a controller using ```php $repository = $this->getDoctrine()->getRepository(Entity::class); ``` everything is fine, regardless of in which mode Symfony runs. However, when trying to get that class from an autowired entity manager within a generic (service) class, like ```php class Service { private ObjectRepository $repository; public function __construct(EntityManagerInterface $em) { $this->repository = $em->getRepository(Entity::class); } /* … */ } ``` Symfony outputs a 500 error **when running in production mode** and writes ``` request.CRITICAL: Uncaught PHP Exception Doctrine\Persistence\Mapping\MappingException: "The class 'App\Entity\MyProject\Entity' was not found in the chain configured namespaces " at /srv/http/myproject/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php line 23 {"exception":"[object] (Doctrine\\Persistence\\Mapping\\MappingException(code: 0): The class 'App\\Entity\\MyProject\\Entity' was not found in the chain configured namespaces at /srv/http/myproject/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:23)"} ``` to `/srv/http/myproject/var/log/prod.log`. For the record, the service class is also autowired in the controller method: ```php class TheController extends AbstractController { /* … */ public function router(string $path, Service $service): Response /* … */ } ``` Everything works as expected, no error is thrown or output in `dev.log`, when `APP_ENV=dev`.
admin closed this issue 2026-01-22 15:37:26 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6716