From 70d756d59c209b2e447b80aed1584d0921d5b65c Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 5 Jun 2011 12:54:29 +0200 Subject: [PATCH] DDC-1184 - Improve error handling in AssignedIdGenerator --- lib/Doctrine/ORM/Id/AssignedGenerator.php | 10 +++++++++- lib/Doctrine/ORM/ORMException.php | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Id/AssignedGenerator.php b/lib/Doctrine/ORM/Id/AssignedGenerator.php index 00aeaaa033e..63c6e5418bc 100644 --- a/lib/Doctrine/ORM/Id/AssignedGenerator.php +++ b/lib/Doctrine/ORM/Id/AssignedGenerator.php @@ -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); + } + // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced. $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value)); } else { @@ -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 { diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index c84dec41ee0..0825ae87d99 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -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 " . + "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)