diff --git a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php index 2774dea68..b9afbfabf 100644 --- a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php +++ b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php @@ -32,7 +32,7 @@ class DefaultRepositoryFactory implements RepositoryFactory /** * The list of EntityRepository instances. * - * @var array<\Doctrine\Common\Persistence\ObjectRepository> + * @var \Doctrine\Common\Persistence\ObjectRepository[] */ private $repositoryList = array(); @@ -41,17 +41,13 @@ class DefaultRepositoryFactory implements RepositoryFactory */ public function getRepository(EntityManagerInterface $entityManager, $entityName) { - $entityName = ltrim($entityName, '\\'); + $className = $entityManager->getClassMetadata($entityName)->getName(); - if (isset($this->repositoryList[$entityName])) { - return $this->repositoryList[$entityName]; + if (isset($this->repositoryList[$className])) { + return $this->repositoryList[$className]; } - $repository = $this->createRepository($entityManager, $entityName); - - $this->repositoryList[$entityName] = $repository; - - return $repository; + return $this->repositoryList[$className] = $this->createRepository($entityManager, $entityName); } /** @@ -64,14 +60,11 @@ class DefaultRepositoryFactory implements RepositoryFactory */ protected function createRepository(EntityManagerInterface $entityManager, $entityName) { + /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */ $metadata = $entityManager->getClassMetadata($entityName); - $repositoryClassName = $metadata->customRepositoryClassName; - - if ($repositoryClassName === null) { - $configuration = $entityManager->getConfiguration(); - $repositoryClassName = $configuration->getDefaultRepositoryClassName(); - } + $repositoryClassName = $metadata->customRepositoryClassName + ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName(); return new $repositoryClassName($entityManager, $metadata); } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 12b941efc..705802306 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -611,6 +611,33 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository"); } + /** + * @group DDC-3257 + */ + public function testSingleRepositoryInstanceForDifferentEntityAliases() + { + $config = $this->_em->getConfiguration(); + + $config->addEntityNamespace('Aliased', 'Doctrine\Tests\Models\CMS'); + $config->addEntityNamespace('AliasedAgain', 'Doctrine\Tests\Models\CMS'); + + $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); + + $this->assertSame($repository, $this->_em->getRepository('Aliased:CmsUser')); + $this->assertSame($repository, $this->_em->getRepository('AliasedAgain:CmsUser')); + } + + /** + * @group DDC-3257 + */ + public function testCanRetrieveRepositoryFromClassNameWithLeadingBackslash() + { + $this->assertSame( + $this->_em->getRepository('\\Doctrine\\Tests\\Models\\CMS\\CmsUser'), + $this->_em->getRepository('Doctrine\\Tests\\Models\\CMS\\CmsUser') + ); + } + /** * @group DDC-1376 *