1
0
mirror of synced 2025-02-07 07:49:27 +03:00

Merge pull request #147 from asm89/assignid

Goetas: Better error handling on missing assigned id
This commit is contained in:
Benjamin Eberlei 2011-10-15 10:41:27 -07:00
commit 7f5844c209
2 changed files with 12 additions and 13 deletions

View File

@ -53,14 +53,14 @@ class AssignedGenerator extends AbstractIdGenerator
if (!$em->getUnitOfWork()->isInIdentityMap($value)) { if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
throw ORMException::entityMissingForeignAssignedId($entity, $value); throw ORMException::entityMissingForeignAssignedId($entity, $value);
} }
// NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced. // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
$identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value)); $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
} else { } else {
$identifier[$idField] = $value; $identifier[$idField] = $value;
} }
} else { } else {
throw ORMException::entityMissingAssignedId($entity); throw ORMException::entityMissingAssignedIdForField($entity, $idField);
} }
} }
} else { } else {
@ -71,17 +71,17 @@ class AssignedGenerator extends AbstractIdGenerator
if (!$em->getUnitOfWork()->isInIdentityMap($value)) { if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
throw ORMException::entityMissingForeignAssignedId($entity, $value); throw ORMException::entityMissingForeignAssignedId($entity, $value);
} }
// NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced. // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
$identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value)); $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
} else { } else {
$identifier[$idField] = $value; $identifier[$idField] = $value;
} }
} else { } else {
throw ORMException::entityMissingAssignedId($entity); throw ORMException::entityMissingAssignedIdForField($entity, $idField);
} }
} }
return $identifier; return $identifier;
} }
} }

View File

@ -34,7 +34,7 @@ class ORMException extends Exception
return new self("It's a requirement to specify a Metadata Driver and pass it ". return new self("It's a requirement to specify a Metadata Driver and pass it ".
"to Doctrine\ORM\Configuration::setMetadataDriverImpl()."); "to Doctrine\ORM\Configuration::setMetadataDriverImpl().");
} }
public static function entityMissingForeignAssignedId($entity, $relatedEntity) public static function entityMissingForeignAssignedId($entity, $relatedEntity)
{ {
return new self( return new self(
@ -46,15 +46,14 @@ class ORMException extends Exception
); );
} }
public static function entityMissingAssignedId($entity) public static function entityMissingAssignedIdForField($entity, $field)
{ {
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 for field '" . $field . "'. " .
"The identifier generation strategy for this entity requires the ID field to be populated before ". "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 " . "EntityManager#persist() is called. If you want automatically generated identifiers instead " .
"you need to adjust the metadata mapping accordingly." "you need to adjust the metadata mapping accordingly."
); );
} }
public static function unrecognizedField($field) public static function unrecognizedField($field)
{ {
return new self("Unrecognized field: $field"); return new self("Unrecognized field: $field");
@ -130,8 +129,8 @@ class ORMException extends Exception
"Unknown Entity namespace alias '$entityNamespaceAlias'." "Unknown Entity namespace alias '$entityNamespaceAlias'."
); );
} }
public static function invalidEntityRepository($className) public static function invalidEntityRepository($className)
{ {
return new self("Invalid repository class '".$className."'. ". return new self("Invalid repository class '".$className."'. ".
"it must be a Doctrine\ORM\EntityRepository."); "it must be a Doctrine\ORM\EntityRepository.");