-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
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.