DDC-379: Implement entity aliases #472

Closed
opened 2026-01-22 12:39:20 +01:00 by admin · 9 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 26, 2010).

Originally assigned to: @jwage on GitHub.

Jira issue originally created by user @jwage:

Similar to how you can use the "use" statement in PHP, it would be nice if DQL had a concept of a default namespace or something.

$q = $em->createQuery('SELECT e FROM Bundle\MyBundle\Entities\TestEntity e');

If I am inside the MyBundle namespace I would want to set something that would allow me to just reference it like this:

$q = $em->createQuery('SELECT e FROM Entities\TestEntity e');
Originally created by @doctrinebot on GitHub (Feb 26, 2010). Originally assigned to: @jwage on GitHub. Jira issue originally created by user @jwage: Similar to how you can use the "use" statement in PHP, it would be nice if DQL had a concept of a default namespace or something. ``` $q = $em->createQuery('SELECT e FROM Bundle\MyBundle\Entities\TestEntity e'); ``` If I am inside the MyBundle namespace I would want to set something that would allow me to just reference it like this: ``` $q = $em->createQuery('SELECT e FROM Entities\TestEntity e'); ```
admin added the New Feature label 2026-01-22 12:39:20 +01:00
admin closed this issue 2026-01-22 12:39:21 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 26, 2010):

Comment created by romanb:

Have you thought about how to implement this? I have thought about this before but I always end up with the conclusion that an implementation would be too cumbersome compared to the minimal "benefit" you get out of this.

@doctrinebot commented on GitHub (Feb 26, 2010): Comment created by romanb: Have you thought about how to implement this? I have thought about this before but I always end up with the conclusion that an implementation would be too cumbersome compared to the minimal "benefit" you get out of this.
Author
Owner

@doctrinebot commented on GitHub (Feb 26, 2010):

Comment created by @beberlei:

The problem here is really that an alias registered in the metadata won't work, since the class and its ocrresponding alias might not been loaded when the alias is requested.

Therefore the only simple and working solution would be to allow an "aliasMap" configuration key inside ORM\Configuration and ClassMetadataFactory checks it like:

    public function getMetadataFor($className)
    {
        if ( ! isset($this->_loadedMetadata[$className])) {
            if (isset($this->_aliasMap[$className])) {
                $className = $this->_aliasMap[$className];
            }

            $cacheKey = "$className\$CLASSMETADATA";
@doctrinebot commented on GitHub (Feb 26, 2010): Comment created by @beberlei: The problem here is really that an alias registered in the metadata won't work, since the class and its ocrresponding alias might not been loaded when the alias is requested. Therefore the only simple and working solution would be to allow an "aliasMap" configuration key inside ORM\Configuration and ClassMetadataFactory checks it like: ``` public function getMetadataFor($className) { if ( ! isset($this->_loadedMetadata[$className])) { if (isset($this->_aliasMap[$className])) { $className = $this->_aliasMap[$className]; } $cacheKey = "$className\$CLASSMETADATA"; ```
Author
Owner

@doctrinebot commented on GitHub (Feb 26, 2010):

Comment created by @jwage:

I have not looked into making something like this work. I realize it might not be something necessarily trivial to implement, and it may not work. I'm just opening the discussion on the topic.

Benjamin, I tried to run with your idea and make something work, but I couldn't get anything working. Did you try it?

I tried something like this:

    // boostrap
    $em = $this->getEntityManager();
    $configuration = $em->getConfiguration();
    $configuration->addEntityAlias('Bundle\GuestbookBundle\Entities\Entry', 'GuestbookEntry');

    // my user code
    $qb = $em->createQueryBuilder()
      ->select('e')
      ->from('GuestbookEntry', 'e');
@doctrinebot commented on GitHub (Feb 26, 2010): Comment created by @jwage: I have not looked into making something like this work. I realize it might not be something necessarily trivial to implement, and it may not work. I'm just opening the discussion on the topic. Benjamin, I tried to run with your idea and make something work, but I couldn't get anything working. Did you try it? I tried something like this: ``` // boostrap $em = $this->getEntityManager(); $configuration = $em->getConfiguration(); $configuration->addEntityAlias('Bundle\GuestbookBundle\Entities\Entry', 'GuestbookEntry'); // my user code $qb = $em->createQueryBuilder() ->select('e') ->from('GuestbookEntry', 'e'); ```
Author
Owner

@doctrinebot commented on GitHub (Feb 26, 2010):

Comment created by @jwage:

Fixed in r7231

@doctrinebot commented on GitHub (Feb 26, 2010): Comment created by @jwage: Fixed in r7231
Author
Owner

@doctrinebot commented on GitHub (Mar 1, 2010):

Comment created by romanb:

Please see my comment on DDC-384. Unless I am missing something the implementation in getMetadataFor is very broken and I dont see a good way to fix it.

@doctrinebot commented on GitHub (Mar 1, 2010): Comment created by romanb: Please see my comment on [DDC-384](http://www.doctrine-project.org/jira/browse/DDC-384). Unless I am missing something the implementation in getMetadataFor is very broken and I dont see a good way to fix it.
Author
Owner

@doctrinebot commented on GitHub (Mar 1, 2010):

Comment created by romanb:

Reopening because caching is severely broken.

@doctrinebot commented on GitHub (Mar 1, 2010): Comment created by romanb: Reopening because caching is severely broken.
Author
Owner

@doctrinebot commented on GitHub (Mar 1, 2010):

Comment created by romanb:

This could probably be fixed by checking the cache again after resolving the fully class name:

            // ...
            if (isset($aliasMap[$className])) {
                $className = $aliasMap[$className];
            }
            // check cache again
            if (isset($this->_loadedMetadata[$className])) {
                return $this->_loadedMetadata[$className];
            }
            //...
@doctrinebot commented on GitHub (Mar 1, 2010): Comment created by romanb: This could probably be fixed by checking the cache again after resolving the fully class name: ``` // ... if (isset($aliasMap[$className])) { $className = $aliasMap[$className]; } // check cache again if (isset($this->_loadedMetadata[$className])) { return $this->_loadedMetadata[$className]; } //... ```
Author
Owner

@doctrinebot commented on GitHub (Mar 2, 2010):

Comment created by @guilhermeblanco:

In r7300 this enhancement was committed.

@doctrinebot commented on GitHub (Mar 2, 2010): Comment created by @guilhermeblanco: In r7300 this enhancement was committed.
Author
Owner

@doctrinebot commented on GitHub (Mar 2, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Mar 2, 2010): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#472