DDC-1184: ORM\Id\AssignedGenerator try to current() on non-array #1489

Closed
opened 2026-01-22 13:16:02 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (May 30, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user slam:

// Doctrine\ORM\AssignedGenerator::generate at line 54

$identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));

$value may also be new, so getEntityIdentifier returns NULL and current(NULL) generates an E_WARNING.

Originally created by @doctrinebot on GitHub (May 30, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user slam: ``` // Doctrine\ORM\AssignedGenerator::generate at line 54 $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value)); ``` `$value` may also be new, so `getEntityIdentifier` returns `NULL` and `current(NULL)` generates an `E_WARNING`.
admin added the Bug label 2026-01-22 13:16:02 +01:00
admin closed this issue 2026-01-22 13:16:02 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jun 5, 2011):

Comment created by @beberlei:

Optimized error handling

@doctrinebot commented on GitHub (Jun 5, 2011): Comment created by @beberlei: Optimized error handling
Author
Owner

@doctrinebot commented on GitHub (Jun 8, 2011):

Comment created by slam:

Error optimizing is welcome but this does not resolve the issue.

Again, if $value is new object, for example a new Entity\User with an empty protected $id:

if (is_object($value)) {
    if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
        throw ORMException::entityMissingForeignAssignedId($entity, $value);
    }

    // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
    $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
} else {
    $identifier[$idField] = $value;
}

will ALWAYS throw an exception in the case the Entity is new.

In my projects, i resolved with

if (is_object($value)) {
    if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
        $identifier[$idField] = null;
    } else {

        // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
        $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
    }
} else {
    $identifier[$idField] = $value;
}

I have not unit-tested the solution (i'm sorry) and I think a more accurate investigation is needed.

@doctrinebot commented on GitHub (Jun 8, 2011): Comment created by slam: Error optimizing is welcome but this does not resolve the issue. Again, if `$value` is new object, for example a `new Entity\User` with an empty `protected $id`: ``` if (is_object($value)) { if (!$em->getUnitOfWork()->isInIdentityMap($value)) { throw ORMException::entityMissingForeignAssignedId($entity, $value); } // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced. $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value)); } else { $identifier[$idField] = $value; } ``` will **ALWAYS** throw an exception in the case the Entity is new. In my projects, i resolved with ``` if (is_object($value)) { if (!$em->getUnitOfWork()->isInIdentityMap($value)) { $identifier[$idField] = null; } else { // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced. $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value)); } } else { $identifier[$idField] = $value; } ``` I have not unit-tested the solution (i'm sorry) and I think a more accurate investigation is needed.
Author
Owner

@doctrinebot commented on GitHub (Jun 11, 2011):

Comment created by @beberlei:

Please read:

http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/composite-primary-keys.html#general-considerations

This is a necessary requirement, that means if you want to create two entities with a composite pk key of the one depending on the other you have to use two flush operations when using MySQL Auto Increment Keys. This will work with PostgreSQL or Oracle Sequences, but not with Auto increment keys.

@doctrinebot commented on GitHub (Jun 11, 2011): Comment created by @beberlei: Please read: http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/composite-primary-keys.html#general-considerations This is a necessary requirement, that means if you want to create two entities with a composite pk key of the one depending on the other you have to use two flush operations when using MySQL Auto Increment Keys. This will work with PostgreSQL or Oracle Sequences, but not with Auto increment keys.
Author
Owner

@doctrinebot commented on GitHub (Jun 11, 2011):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jun 11, 2011): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Jun 11, 2011):

Comment created by slam:

Ok, roger.

Just one note: you must know that both two entities are STATE_NEW, because if you try to getEntityState($dependentEntity), UnitOfWork.php:2155 will throw a E_RECOVERABLE_ERROR because the $independentEntity could not be converted to a string (because it has no id).

@doctrinebot commented on GitHub (Jun 11, 2011): Comment created by slam: Ok, roger. Just one note: you must know that both two entities are STATE_NEW, because if you try to `getEntityState($dependentEntity)`, `UnitOfWork.php:2155` will throw a E_RECOVERABLE_ERROR because the $independentEntity could not be converted to a string (because it has no id).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1489