[2.0] Adding support for entity aliases in the query language
This commit is contained in:
parent
50e9d8c547
commit
7c81b0b003
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user