diff --git a/lib/Doctrine/Common/Annotations/AnnotationReader.php b/lib/Doctrine/Common/Annotations/AnnotationReader.php index e44b11b00..f23236bde 100644 --- a/lib/Doctrine/Common/Annotations/AnnotationReader.php +++ b/lib/Doctrine/Common/Annotations/AnnotationReader.php @@ -70,7 +70,7 @@ class AnnotationReader public function __construct(Cache $cache = null) { $this->_parser = new Parser; - $this->_cache = $cache ?: new Doctrine\Common\Cache\ArrayCache; + $this->_cache = $cache ?: new \Doctrine\Common\Cache\ArrayCache; } /** diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index b0df3bb14..b52542d05 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -51,7 +51,8 @@ class Configuration extends \Doctrine\DBAL\Configuration 'namedQueries' => array(), 'namedNativeQueries' => array(), 'autoGenerateProxyClasses' => true, - 'proxyNamespace' => null + 'proxyNamespace' => null, + 'entityAliasMap' => array() )); } @@ -119,6 +120,38 @@ class Configuration extends \Doctrine\DBAL\Configuration $this->_attributes['metadataDriverImpl'] = $driverImpl; } + /** + * Add an alias for an entity. + * + * @param string $className + * @param string $alias + */ + public function addEntityAlias($className, $alias) + { + $this->_attributes['entityAliasMap'][$alias] = $className; + } + + /** + * get the array of entity aliases + * + * @return array $aliasMap + */ + public function getEntityAliasMap() + { + return $this->_attributes['entityAliasMap']; + } + + /** + * Set the entity alias map + * + * @param array $entityAliasMap + * @return void + */ + public function setEntityAliasMap(array $entityAliasMap) + { + $this->_attributes['entityAliasMap'] = $entityAliasMap; + } + /** * Gets the cache driver implementation that is used for the mapping metadata. * diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 5117831e4..5bb495c73 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -128,6 +128,10 @@ class ClassMetadataFactory public function getMetadataFor($className) { if ( ! isset($this->_loadedMetadata[$className])) { + $aliasMap = $this->_em->getConfiguration()->getEntityAliasMap(); + if (isset($aliasMap[$className])) { + $className = $aliasMap[$className]; + } $cacheKey = "$className\$CLASSMETADATA"; if ($this->_cacheDriver) { if (($cached = $this->_cacheDriver->fetch($cacheKey)) !== false) { diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index a8767e539..eae389828 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -867,6 +867,10 @@ class Parser $this->match(Lexer::T_IDENTIFIER); $schemaName = $this->_lexer->token['value']; + $aliasMap = $this->_em->getConfiguration()->getEntityAliasMap(); + if (isset($aliasMap[$schemaName])) { + $schemaName = $aliasMap[$schemaName]; + } $exists = class_exists($schemaName, true); if ( ! $exists) { diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php index 2b20e1667..0cc8ce003 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryTest.php @@ -226,5 +226,19 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a") ->getSingleResult(); } -} + public function testSupportsQueriesWithEntityAliases() + { + $this->_em->getConfiguration()->addEntityAlias('Doctrine\Tests\Models\CMS\CmsUser', 'TestAlias'); + + try { + $query = $this->_em->createQuery('UPDATE TestAlias u SET u.name = ?1'); + $this->assertEquals('UPDATE cms_users SET name = ?', $query->getSql()); + $query->free(); + } catch (\Exception $e) { + $this->fail($e->getMessage()); + } + + $this->_em->getConfiguration()->setEntityAliasMap(array()); + } +} \ No newline at end of file