1
0
mirror of synced 2025-01-10 02:57:10 +03:00

Merge branch 'hotfix/#1112-single-repository-for-aliased-entity'

Close #1112
This commit is contained in:
Marco Pivetta 2014-08-18 15:22:13 +02:00
commit c20b3a7cf2
2 changed files with 36 additions and 16 deletions

View File

@ -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,13 +60,10 @@ 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);
}

View File

@ -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
*