[PR #11601] EntityManager::getReference() should handle a PK which is also a FK #13124

Open
opened 2026-01-22 16:16:15 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/11601

State: closed
Merged: No


EntityManager::getReference() should be able to handle a PK which is also a FK, just the way ::find() already does.


class Entity
{
  #[Id]
  #[ManyToOne(targetEntity: Related::class)]
  public Related $related
}

Current behaviour:

// case 1: by PK value
$entityManager->getReference(Entity::class, 1);
// Cannot assign int to property Entity::$related of type Related
// case 2: by reference
$related = $entityManager->getReference(Related::class, 1]);
$entityManager->getReference(Entity::class, $related);
// Object of class Related could not be converted to string

With this PR, the proxy factory would hydrate the proxy with a reference to the related entity if a relation is detected (addressing case 1 above)
Additionally EntityManager::getReference() would use the same approach as in find() and attempt to extract the underlying PK value when passed an entity (case 2).

In this first attempt, I just copy/pasted some code from find() to getReference(). These two have a lot in common, if this proposed change is of any interest I will improve it and deduplicate this code in a second version.

P.S. I believe this (somewhat related) issue #5640 may be closed.

**Original Pull Request:** https://github.com/doctrine/orm/pull/11601 **State:** closed **Merged:** No --- `EntityManager::getReference()` should be able to handle a PK which is also a FK, just the way `::find()` already does. ```php class Entity { #[Id] #[ManyToOne(targetEntity: Related::class)] public Related $related } ``` Current behaviour: ```php // case 1: by PK value $entityManager->getReference(Entity::class, 1); // Cannot assign int to property Entity::$related of type Related ``` ```php // case 2: by reference $related = $entityManager->getReference(Related::class, 1]); $entityManager->getReference(Entity::class, $related); // Object of class Related could not be converted to string ``` With this PR, the proxy factory would hydrate the proxy with a reference to the related entity if a relation is detected (addressing case 1 above) Additionally `EntityManager::getReference()` would use the same approach as in `find()` and attempt to extract the underlying PK value when passed an entity (case 2). In this first attempt, I just copy/pasted some code from `find()` to `getReference()`. These two have a lot in common, if this proposed change is of any interest I will improve it and deduplicate this code in a second version. _P.S. I believe this (somewhat related) issue #5640 may be closed._
admin added the pull-request label 2026-01-22 16:16:15 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#13124