From d24f2881496fcf0faa6c7a1a22f07e3fc001d935 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Thu, 29 Sep 2011 09:31:06 +0200 Subject: [PATCH 1/2] Better error handling on missing assigned id --- lib/Doctrine/ORM/Id/AssignedGenerator.php | 8 ++++---- lib/Doctrine/ORM/ORMException.php | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/Doctrine/ORM/Id/AssignedGenerator.php b/lib/Doctrine/ORM/Id/AssignedGenerator.php index 05c3790af..0143a157f 100644 --- a/lib/Doctrine/ORM/Id/AssignedGenerator.php +++ b/lib/Doctrine/ORM/Id/AssignedGenerator.php @@ -53,14 +53,14 @@ class AssignedGenerator extends AbstractIdGenerator 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; } } else { - throw ORMException::entityMissingAssignedId($entity); + throw ORMException::entityMissingAssignedIdForField($entity, $idField); } } } else { @@ -71,7 +71,7 @@ class AssignedGenerator extends AbstractIdGenerator 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 { @@ -81,7 +81,7 @@ class AssignedGenerator extends AbstractIdGenerator throw ORMException::entityMissingAssignedId($entity); } } - + return $identifier; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index aa9657552..b28c8d32d 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -34,7 +34,7 @@ class ORMException extends Exception 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( @@ -50,11 +50,18 @@ class ORMException extends Exception { 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 " . + "EntityManager#persist() is called. If you want automatically generated identifiers instead " . + "you need to adjust the metadata mapping accordingly." + ); + } + public static function entityMissingAssignedIdForField($entity, $field) + { + 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 ". + "EntityManager#persist() is called. If you want automatically generated identifiers instead " . "you need to adjust the metadata mapping accordingly." ); } - public static function unrecognizedField($field) { return new self("Unrecognized field: $field"); @@ -130,8 +137,8 @@ class ORMException extends Exception "Unknown Entity namespace alias '$entityNamespaceAlias'." ); } - - public static function invalidEntityRepository($className) + + public static function invalidEntityRepository($className) { return new self("Invalid repository class '".$className."'. ". "it must be a Doctrine\ORM\EntityRepository."); From fdb9fb1c2b5d35974188eb9344d87e0a41c75ca8 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 15 Oct 2011 19:23:36 +0200 Subject: [PATCH 2/2] AssignedGenerator can always tell what field is missing an id --- lib/Doctrine/ORM/Id/AssignedGenerator.php | 4 ++-- lib/Doctrine/ORM/ORMException.php | 8 -------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/lib/Doctrine/ORM/Id/AssignedGenerator.php b/lib/Doctrine/ORM/Id/AssignedGenerator.php index 0143a157f..90a35fa12 100644 --- a/lib/Doctrine/ORM/Id/AssignedGenerator.php +++ b/lib/Doctrine/ORM/Id/AssignedGenerator.php @@ -78,10 +78,10 @@ class AssignedGenerator extends AbstractIdGenerator $identifier[$idField] = $value; } } else { - throw ORMException::entityMissingAssignedId($entity); + throw ORMException::entityMissingAssignedIdForField($entity, $idField); } } return $identifier; } -} \ No newline at end of file +} diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index b28c8d32d..c156893c5 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -46,14 +46,6 @@ class ORMException extends Exception ); } - public static function entityMissingAssignedId($entity) - { - 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 entityMissingAssignedIdForField($entity, $field) { return new self("Entity of type " . get_class($entity) . " is missing an assigned ID for field '" . $field . "'. " .