Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1184

ORM\Id\AssignedGenerator try to current() on non-array

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 2.1
    • Fix Version/s: 2.1
    • Component/s: None
    • Security Level: All
    • Labels:
      None

      Description

      // 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.

        Activity

        Hide
        Benjamin Eberlei added a comment -

        Optimized error handling

        Show
        Benjamin Eberlei added a comment - Optimized error handling
        Hide
        Slam added a comment - - edited

        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.

        Show
        Slam added a comment - - edited 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.
        Hide
        Benjamin Eberlei added a comment -

        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.

        Show
        Benjamin Eberlei added a comment - 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.
        Hide
        Slam added a comment -

        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).

        Show
        Slam added a comment - 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).

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Slam
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: