From 2c58207e6e69be5b6527eb7d88412f4d9a3f40bc Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Fri, 8 Jun 2018 16:05:54 +0200 Subject: [PATCH] Better error message when missing entity mapping Otherwise you just get an error like > php.CRITICAL: Call to a member function getClassMetadata() on null --- Repository/ServiceEntityRepository.php | 8 ++++++++ .../ServiceEntityRepositoryTest.php | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 Tests/Repository/ServiceEntityRepositoryTest.php diff --git a/Repository/ServiceEntityRepository.php b/Repository/ServiceEntityRepository.php index 0b36f233d..5d4597fa2 100644 --- a/Repository/ServiceEntityRepository.php +++ b/Repository/ServiceEntityRepository.php @@ -4,6 +4,7 @@ use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\ORM\EntityRepository; +use LogicException; /** * Optional EntityRepository base class with a simplified constructor (for autowiring). @@ -28,6 +29,13 @@ public function __construct(ManagerRegistry $registry, $entityClass) { $manager = $registry->getManagerForClass($entityClass); + if (null === $manager) { + throw new LogicException(sprintf( + 'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.', + $entityClass + )); + } + parent::__construct($manager, $manager->getClassMetadata($entityClass)); } } diff --git a/Tests/Repository/ServiceEntityRepositoryTest.php b/Tests/Repository/ServiceEntityRepositoryTest.php new file mode 100644 index 000000000..499c99b44 --- /dev/null +++ b/Tests/Repository/ServiceEntityRepositoryTest.php @@ -0,0 +1,20 @@ +getMockBuilder(ManagerRegistry::class)->getMock(); + new ServiceEntityRepository($registry, TestEntity::class); + } +}