Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/025caa587d88293d9c09 to your computer and use it in GitHub Desktop.
Save doctrinebot/025caa587d88293d9c09 to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-1243 - https://github.com/doctrine/doctrine2/issues/1852
diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
index 8bfda64..476fbb8 100644
--- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
+++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
@@ -229,6 +229,27 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
}
/**
+ * Does the given entity class have an inherited identifier?
+ *
+ * @param string $name
+ * @return true or false
+ */
+ protected function hasInheritedIdentifier($name)
+ {
+ // Collect parent classes, ignoring transient (not-mapped) classes
+ // to see if they have an identifier
+ foreach (array_reverse(class_parents($name)) as $parentClass) {
+ if ( ! $this->driver->isTransient($parentClass)) {
+ $parent = $this->loadedMetadata[$parentClass];
+ if (!$parent->isInheritanceTypeNone() && $parent->identifier) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
* Loads the metadata of the class in question and all it's ancestors whose metadata
* is still not loaded.
*
@@ -314,7 +335,7 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
}
// Verify & complete identifier mapping
- if ( ! $class->identifier && ! $class->isMappedSuperclass) {
+ if ( ! $class->identifier && ! $class->isMappedSuperclass && ! $this->hasInheritedIdentifier($name)) {
throw MappingException::identifierRequired($className);
}
diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
index 8bfda64..2c0838b 100644
--- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
+++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
@@ -229,6 +229,27 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
}
/**
+ * Does the given entity class have an inherited identifier?
+ *
+ * @param string $name
+ * @return true or false
+ */
+ protected function hasInheritedIdentifier($name)
+ {
+ // Collect parent classes, ignoring transient (not-mapped) classes
+ // to see if they have an identifier
+ foreach (array_reverse(class_parents($name)) as $parentClass) {
+ if ( ! $this->driver->isTransient($parentClass) && ! empty($this->loadedMetadata[$parentClass])) {
+ $parent = $this->loadedMetadata[$parentClass];
+ if (!$parent->isInheritanceTypeNone() && $parent->identifier) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
* Loads the metadata of the class in question and all it's ancestors whose metadata
* is still not loaded.
*
@@ -314,7 +335,7 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
}
// Verify & complete identifier mapping
- if ( ! $class->identifier && ! $class->isMappedSuperclass) {
+ if ( ! $class->identifier && ! $class->isMappedSuperclass && ! $this->hasInheritedIdentifier($name)) {
throw MappingException::identifierRequired($className);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment