Skip to content

Remove embedded documents complexity #782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
$class->setDefaultDiscriminatorValue($parent->defaultDiscriminatorValue);
$class->setIdGeneratorType($parent->generatorType);
$this->addInheritedFields($class, $parent);
$this->addInheritedRelations($class, $parent);
$this->addInheritedIndexes($class, $parent);
$class->setIdentifier($parent->identifier);
$class->setVersioned($parent->isVersioned);
Expand Down Expand Up @@ -295,6 +296,34 @@ private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $pare
}
}


/**
* Adds inherited association mappings to the subclass mapping.
*
* @param \Doctrine\ORM\Mapping\ClassMetadata $subClass
* @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass
*
* @return void
*
* @throws MappingException
*/
private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass)
{
foreach ($parentClass->associationMappings as $field => $mapping) {
if ($parentClass->isMappedSuperclass) {
$mapping['sourceDocument'] = $subClass->name;
}

if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) {
$mapping['inherited'] = $parentClass->name;
}
if ( ! isset($mapping['declared'])) {
$mapping['declared'] = $parentClass->name;
}
$subClass->addInheritedAssociationMapping($mapping);
}
}

/**
* Adds inherited indexes to the subclass mapping.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,22 @@ public function addInheritedFieldMapping(array $fieldMapping)
}
}

/**
* INTERNAL:
* Adds an association mapping without completing/validating it.
* This is mainly used to add inherited association mappings to derived classes.
*
* @param array $mapping
*
* @return void
*
* @throws MappingException
*/
public function addInheritedAssociationMapping(array $mapping/*, $owningClassName = null*/)
{
$this->associationMappings[$mapping['fieldName']] = $mapping;
}

/**
* Checks whether the class has a mapped association with the given field name.
*
Expand Down
17 changes: 6 additions & 11 deletions lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public function prepareUpdateData($document)
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_MANY) {
// Do nothing right now
}
// @ReferenceMany is handled by CollectionPersister
}
return $updateData;
}
Expand Down Expand Up @@ -278,6 +279,7 @@ public function prepareUpsertData($document)
$updateData['$set'][$mapping['name']] = (is_null($new) ? null : $this->prepareReferencedDocumentValue($mapping, $new));
}
}
// @EmbedMany and @ReferenceMany are handled by CollectionPersister
}

// add discriminator if the class has one
Expand All @@ -302,13 +304,7 @@ public function prepareUpsertData($document)
*/
public function prepareReferencedDocumentValue(array $referenceMapping, $document)
{
try {
return $this->dm->createDBRef($document, $referenceMapping);
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException(
sprintf('You are trying to reference a non-object in %s field, "%s" given', $referenceMapping['name'], $document)
);
}
return $this->dm->createDBRef($document, $referenceMapping);
}

/**
Expand Down Expand Up @@ -362,10 +358,9 @@ public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDo

case ClassMetadata::EMBED_MANY:
case ClassMetadata::REFERENCE_MANY:
// Skip PersistentCollections already scheduled for deletion/update
if ( ! $includeNestedCollections && $rawValue instanceof PersistentCollection &&
($this->uow->isCollectionScheduledForDeletion($rawValue) ||
$this->uow->isCollectionScheduledForUpdate($rawValue))) {
// Skip PersistentCollections already scheduled for deletion
if ( ! $includeNestedCollections && $rawValue instanceof PersistentCollection
&& $this->uow->isCollectionScheduledForDeletion($rawValue)) {
break;
}

Expand Down
Loading