From 07ef1db5add441b8ce47cb89d7574f73dfcce9c0 Mon Sep 17 00:00:00 2001 From: lsmith77 Date: Fri, 10 Feb 2012 21:39:53 +0100 Subject: [PATCH] more fixes related to inheritance mapping, added support for declared mapping --- .../ODM/PHPCR/Mapping/ClassMetadata.php | 25 +++++++++++++-- .../PHPCR/Mapping/ClassMetadataFactory.php | 31 ++++++++++++------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadata.php b/lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadata.php index 951179eb8..c28476fe2 100644 --- a/lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadata.php @@ -38,7 +38,6 @@ */ class ClassMetadata implements ClassMetadataInterface { - const TO_ONE = 5; const TO_MANY = 10; const ONE_TO_ONE = 1; @@ -262,6 +261,11 @@ class ClassMetadata implements ClassMetadataInterface */ private $inheritedFields = array(); + /** + * @var array + */ + private $declaredFields = array(); + /** * Initializes a new ClassMetadata instance that will hold the object-document mapping * metadata of the class with the given name. @@ -463,9 +467,14 @@ public function mapId(array $mapping) $this->validateAndCompleteFieldMapping($mapping, false); } - public function setFieldInherited($fieldName) + public function setFieldInherited($fieldName, $className) + { + $this->inheritedFields[$fieldName] = $className; + } + + public function setDeclaredInherited($fieldName, $className) { - $this->inheritedFields[$fieldName] = true; + $this->declaredFields[$fieldName] = $className; } public function mapNode(array $mapping) @@ -814,6 +823,16 @@ public function isInheritedField($fieldName) return isset($this->inheritedFields[$fieldName]); } + /** + * Checks whether a mapped field is declared previously. + * + * @return boolean TRUE if the field is declared, FALSE otherwise. + */ + public function isDeclaredField($fieldName) + { + return isset($this->declaredFields[$fieldName]); + } + /** * Map a field. * diff --git a/lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadataFactory.php index 9f4e68392..a3bf59bcb 100644 --- a/lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadataFactory.php @@ -138,51 +138,60 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound) private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) { foreach ($parentClass->fieldMappings as $fieldName => $mapping) { - $subClass->setFieldInherited($fieldName); + $this->registerParentOnField($subClass, $parentClass, $fieldName); $subClass->mapField($mapping); } foreach ($parentClass->associationsMappings as $fieldName => $mapping) { - $subClass->setFieldInherited($fieldName); + $this->registerParentOnField($subClass, $parentClass, $fieldName); $subClass->storeAssociationMapping($mapping); } foreach ($parentClass->childMappings as $fieldName => $mapping) { - $subClass->setFieldInherited($fieldName); + $this->registerParentOnField($subClass, $parentClass, $fieldName); $subClass->mapChild($mapping); } foreach ($parentClass->childrenMappings as $fieldName => $mapping) { - $subClass->setFieldInherited($fieldName); + $this->registerParentOnField($subClass, $parentClass, $fieldName); $subClass->mapChildren($mapping); } foreach ($parentClass->referrersMappings as $fieldName => $mapping) { - $subClass->setFieldInherited($fieldName); + $this->registerParentOnField($subClass, $parentClass, $fieldName); $subClass->mapReferrers($mapping); } if ($parentClass->node) { - $subClass->setFieldInherited($parentClass->node); + $this->registerParentOnField($subClass, $parentClass, $parentClass->node); $subClass->mapNode(array('fieldName' => $parentClass->node)); } if ($parentClass->nodename) { - $subClass->setFieldInherited($parentClass->nodename); + $this->registerParentOnField($subClass, $parentClass, $parentClass->nodename); $subClass->mapNodename(array('fieldName' => $parentClass->nodename)); } if ($parentClass->parentMapping) { - $subClass->setFieldInherited($parentClass->parentMapping); + $this->registerParentOnField($subClass, $parentClass, $parentClass->parentMapping); $subClass->mapParentDocument(array('fieldName' => $parentClass->parentMapping)); } if ($parentClass->localeMapping) { - $subClass->setFieldInherited($parentClass->localeMapping); + $this->registerParentOnField($subClass, $parentClass, $parentClass->localeMapping); $subClass->mapLocale(array('fieldName' => $parentClass->localeMapping)); } if ($parentClass->versionNameField) { - $subClass->setFieldInherited($parentClass->versionNameField); + $this->registerParentOnField($subClass, $parentClass, $parentClass->versionNameField); $subClass->mapVersionName(array('fieldName' => $parentClass->versionNameField)); } if ($parentClass->versionCreatedField) { - $subClass->setFieldInherited($parentClass->versionCreatedField); + $this->registerParentOnField($subClass, $parentClass, $parentClass->versionCreatedField); $subClass->mapVersionCreated(array('fieldName' => $parentClass->versionCreatedField)); } } + private function registerParentOnField(ClassMetadata $subClass, ClassMetadata $parentClass, $fieldName) + { + if (!$parentClass->isInheritedField($fieldName) && !$parentClass->isMappedSuperclass) { + $subClass->setFieldInherited($fieldName, $parentClass->name); + } + if (!$parentClass->isDeclaredField($fieldName)) { + $subClass->setDeclaredInherited($fieldName, $parentClass->name); + } + } /** * {@inheritdoc} */