Skip to content

Commit

Permalink
Better error message when missing entity mapping
Browse files Browse the repository at this point in the history
Otherwise you just get an error like

> php.CRITICAL: Call to a member function getClassMetadata() on null
  • Loading branch information
Tobion committed Oct 28, 2018
1 parent 3ed47e7 commit 297f257
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Repository/ServiceEntityRepository.php
Expand Up @@ -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).
Expand All @@ -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));
}
}
20 changes: 20 additions & 0 deletions Tests/Repository/ServiceEntityRepositoryTest.php
@@ -0,0 +1,20 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Tests\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry
use PHPUnit\Framework\TestCase;

class ServiceEntityRepositoryTest extends TestCase
{
/**
* @expectedException \LogicException
* @expectedExceptionMessage Could not find the entity manager for class "Doctrine\Bundle\DoctrineBundle\Tests\Repository\TestEntity". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.
*/
public function testConstructorThrowsExceptionWhenNoManagerFound()
{
$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
new ServiceEntityRepository($registry, TestEntity::class);
}
}

0 comments on commit 297f257

Please sign in to comment.