Skip to content

Commit

Permalink
DDC-1184 - Improve error handling in AssignedIdGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Jun 5, 2011
1 parent f367755 commit 70d756d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/Doctrine/ORM/Id/AssignedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public function generate(EntityManager $em, $entity)
if ($class->isIdentifierComposite) {
$idFields = $class->getIdentifierFieldNames();
foreach ($idFields as $idField) {
$value = $class->getReflectionProperty($idField)->getValue($entity);
$value = $class->reflFields[$idField]->getValue($entity);
if (isset($value)) {
if (is_object($value)) {
if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
throw ORMException::entityMissingForeignAssignedId($entity, $value);

This comment has been minimized.

Copy link
@realmfoo

realmfoo Aug 2, 2011

Cascade persist doesn't work if associated object relies on parents auto generated ID, even if it has only one primary key!

}

// NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
$identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
} else {
Expand All @@ -64,6 +68,10 @@ public function generate(EntityManager $em, $entity)
$value = $class->reflFields[$idField]->getValue($entity);
if (isset($value)) {
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 {
Expand Down
17 changes: 16 additions & 1 deletion lib/Doctrine/ORM/ORMException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,25 @@ public static function missingMappingDriverImpl()
return new self("It's a requirement to specify a Metadata Driver and pass it ".
"to Doctrine\ORM\Configuration::setMetadataDriverImpl().");
}

public static function entityMissingForeignAssignedId($entity, $relatedEntity)
{
return new self(
"Entity of type " . get_class($entity) . " has identity through a foreign entity " . get_class($relatedEntityClass) . ", " .
"however this entity has no ientity itself. You have to call EntityManager#persist() on the related entity " .

This comment has been minimized.

Copy link
@guilhermeblanco

guilhermeblanco Aug 2, 2011

Member

"... however this entity has no identity..."

You missed the "D" in identity.

"and make sure it an identifier was generated before trying to persist '" . get_class($entity) . "'. In case " .
"of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call " .
"EntityManager#flush() between both persist operations."
);
}

public static function entityMissingAssignedId($entity)
{
return new self("Entity of type " . get_class($entity) . " is missing an assigned ID.");
return new self("Entity of type " . get_class($entity) . " is missing an assigned ID. " .
"The identifier generation strategy for this entity requires the ID field to be populated before ".
"EntityManager#persist() is called. If you want automatically generated identifiers instead " .
"you need to adjust the metadata mapping accordingly."
);
}

public static function unrecognizedField($field)
Expand Down

0 comments on commit 70d756d

Please sign in to comment.