DDC-687: Add New Entity Attribute "idGetter" to allow accessing the ID without triggering lazy-load #845

Closed
opened 2026-01-22 12:52:32 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 12, 2010).

Jira issue originally created by user @beberlei:

Often people present us with the use-case that they want to access the ID of a proxy without loading it.

This has lead to several ugly solutions like mapping the ID to an object and as a foreign key field. There currently exists a simple solution for this:

$id = $em->getUnitOfWork()->getEntityIdentifier($entity->getRelatedProxy());

However we could add a new property here called "idGetter" that would take the name of a method.

During Proxy Generation then this method is created with magic functionality that:

  1. In case of Single Primary Key returns the single value
  2. In case of Composite Primary Key returns an array of the values in their UoW internal order
  3. Throw an Exception if the method does not exist on the original object
Originally created by @doctrinebot on GitHub (Jul 12, 2010). Jira issue originally created by user @beberlei: Often people present us with the use-case that they want to access the ID of a proxy without loading it. This has lead to several ugly solutions like mapping the ID to an object and as a foreign key field. There currently exists a simple solution for this: ``` $id = $em->getUnitOfWork()->getEntityIdentifier($entity->getRelatedProxy()); ``` However we could add a new property here called "idGetter" that would take the name of a method. During Proxy Generation then this method is created with magic functionality that: 1. In case of Single Primary Key returns the single value 2. In case of Composite Primary Key returns an array of the values in their UoW internal order 3. Throw an Exception if the method does not exist on the original object
admin added the New Feature label 2026-01-22 12:52:32 +01:00
admin closed this issue 2026-01-22 12:52:33 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 25, 2011):

Comment created by stefanklug:

What about an @IdGetter annotation. A function instrumented like this would not trigger the lazy load within the proxy.

Something like

class Entity {
    /*** @Id ***/
    private $id;

    /*** @IdGetter ***/
    public function getId() {
       return $this->id;
    }
}

would then result in the proxy implementation

class EntityProxy extends Entity {

    public function getId() {
        if (!$this->*_isInitialized_*) {
            return $this->_identifier;
         } else {
             return parent::getId();
        }
    }
}

After reading the original post I realized that it proposed nearly the same thing. Nevertheless I'll leave it here for clarity. I still think that an annotation on a function would be better, than an annotation which gets the function name as a parameter.

Regards Stefan

@doctrinebot commented on GitHub (Jan 25, 2011): Comment created by stefanklug: What about an @IdGetter annotation. A function instrumented like this would not trigger the lazy load within the proxy. Something like ``` class Entity { /*** @Id ***/ private $id; /*** @IdGetter ***/ public function getId() { return $this->id; } } ``` would then result in the proxy implementation ``` class EntityProxy extends Entity { public function getId() { if (!$this->*_isInitialized_*) { return $this->_identifier; } else { return parent::getId(); } } } ``` After reading the original post I realized that it proposed nearly the same thing. Nevertheless I'll leave it here for clarity. I still think that an annotation on a function would be better, than an annotation which gets the function name as a parameter. Regards Stefan
Author
Owner

@doctrinebot commented on GitHub (Jan 25, 2011):

Comment created by @beberlei:

$this->_identifier is an array.

@doctrinebot commented on GitHub (Jan 25, 2011): Comment created by @beberlei: $this->_identifier is an array.
Author
Owner

@doctrinebot commented on GitHub (Sep 11, 2013):

Comment created by @beberlei:

This was implemented in 2.2 by detecting when this can be done on a getId() call automatically.

@doctrinebot commented on GitHub (Sep 11, 2013): Comment created by @beberlei: This was implemented in 2.2 by detecting when this can be done on a getId() call automatically.
Author
Owner

@doctrinebot commented on GitHub (Sep 11, 2013):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Sep 11, 2013): 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#845