Skip to content

Loading…

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

Closed
doctrinebot opened this Issue · 5 comments

2 participants

@doctrinebot

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.

@doctrinebot

Comment created by @beberlei:

Optimized error handling

@doctrinebot

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

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

Issue was closed with resolution "Invalid"

@doctrinebot

Comment created by slam:

Ok, roger.

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

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.1 milestone
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.