DDC-3736: Support for Objects as Identifiers with Strategy AUTO #4585

Open
opened 2026-01-22 14:45:28 +01:00 by admin · 1 comment
Owner

Originally created by @doctrinebot on GitHub (May 14, 2015).

Jira issue originally created by user kezek:

According to the official documentation (http://doctrine-orm.readthedocs.org/en/latest/changelog/migration25.html#support-for-objects-as-identifiers) using objects as Identifiers work .

The issue is that if in the mapping settings generator strategy is set to AUTO, the BasicEntityPersister won't hydrate the id to an Object , but keep it as a scalar.

\\Doctrine\ORM\Persisters\Entity::executeInserts() , line 257
$generatedId = $idGenerator->generate($this->em, $entity);
$id = array(
    $this->class->identifier[0] => $generatedId
);
$postInsertIds[] = array(
    'generatedId' => $generatedId,
    'entity' => $entity,
);

This could be tracked down to the IdGenerator :

\\Doctrine\ORM\Id\IdentityGenerator::generate() , line 53
public function generate(
        EntityManager $em, $entity)
    {
        return (int)$em->getConnection()->lastInsertId($this->sequenceName);
    }
Originally created by @doctrinebot on GitHub (May 14, 2015). Jira issue originally created by user kezek: According to the official documentation (http://doctrine-orm.readthedocs.org/en/latest/changelog/migration*2*5.html#support-for-objects-as-identifiers) using objects as Identifiers work . The issue is that if in the mapping settings generator strategy is set to AUTO, the BasicEntityPersister won't hydrate the id to an Object , but keep it as a scalar. ``` \\Doctrine\ORM\Persisters\Entity::executeInserts() , line 257 $generatedId = $idGenerator->generate($this->em, $entity); $id = array( $this->class->identifier[0] => $generatedId ); $postInsertIds[] = array( 'generatedId' => $generatedId, 'entity' => $entity, ); ``` This could be tracked down to the IdGenerator : ``` \\Doctrine\ORM\Id\IdentityGenerator::generate() , line 53 public function generate( EntityManager $em, $entity) { return (int)$em->getConnection()->lastInsertId($this->sequenceName); } ```
admin added the Improvement label 2026-01-22 14:45:28 +01:00
Author
Owner

@doctrinebot commented on GitHub (May 14, 2015):

Comment created by kezek:

Workaround:

id:
        id:
            type: UserId
            column: id
            generator:
                strategy: CUSTOM
            customIdGenerator:
                class: MyApp\ORM\Id\ObjectIdentityGenerator

Create a ObjectIdentityGenerator that extends AbstractIdGenerator and implement generate:

public function generate(
        EntityManager $em, $entity)
    {
        $entityClass = get_class($entity);
        $idFieldName = $em->getClassMetadata($entityClass)->getSingleIdentifierColumnName();
        $reflection = new ReflectionClass($entityClass);
        $params = $reflection->getConstructor()->getParameters();
        foreach ($params AS $param) {
            if ($param->getName() === $idFieldName && $idClass = $param->getClass()) {
                return new $idClass->name((int) $em->getConnection()->lastInsertId($this->sequenceName));
            }
        }
        return (int)$em->getConnection()->lastInsertId($this->sequenceName);
    }
@doctrinebot commented on GitHub (May 14, 2015): Comment created by kezek: Workaround: ``` id: id: type: UserId column: id generator: strategy: CUSTOM customIdGenerator: class: MyApp\ORM\Id\ObjectIdentityGenerator ``` Create a ObjectIdentityGenerator that extends AbstractIdGenerator and implement generate: ``` public function generate( EntityManager $em, $entity) { $entityClass = get_class($entity); $idFieldName = $em->getClassMetadata($entityClass)->getSingleIdentifierColumnName(); $reflection = new ReflectionClass($entityClass); $params = $reflection->getConstructor()->getParameters(); foreach ($params AS $param) { if ($param->getName() === $idFieldName && $idClass = $param->getClass()) { return new $idClass->name((int) $em->getConnection()->lastInsertId($this->sequenceName)); } } return (int)$em->getConnection()->lastInsertId($this->sequenceName); } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4585