-
-
Notifications
You must be signed in to change notification settings - Fork 505
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
fix: Don't process subclass indexes with SingleCollection inheritance #2573
base: 2.10.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,12 @@ public function updateIndexes(?int $maxTimeMs = null, ?WriteConcern $writeConcer | |
continue; | ||
} | ||
|
||
if ($class->isInheritanceTypeSingleCollection() && count($class->parentClasses) > 0) { | ||
// Skip document nodes that use the same collection as one of their parents. | ||
// Indexes will be added by the parent document. | ||
continue; | ||
} | ||
|
||
$this->updateDocumentIndexes($class->name, $maxTimeMs, $writeConcern); | ||
} | ||
} | ||
|
@@ -172,56 +178,68 @@ private function doGetDocumentIndexes(string $documentName, array &$visited): ar | |
return []; | ||
} | ||
|
||
$visited[$documentName] = true; | ||
|
||
$class = $this->dm->getClassMetadata($documentName); | ||
$indexes = $this->prepareIndexes($class); | ||
$embeddedDocumentIndexes = []; | ||
$class = $this->dm->getClassMetadata($documentName); | ||
$processClasses = [$class]; | ||
|
||
// Add indexes from embedded & referenced documents | ||
foreach ($class->fieldMappings as $fieldMapping) { | ||
if (isset($fieldMapping['embedded'])) { | ||
if (isset($fieldMapping['targetDocument'])) { | ||
$possibleEmbeds = [$fieldMapping['targetDocument']]; | ||
} elseif (isset($fieldMapping['discriminatorMap'])) { | ||
$possibleEmbeds = array_unique($fieldMapping['discriminatorMap']); | ||
} else { | ||
continue; | ||
} | ||
if ($class->isInheritanceTypeSingleCollection()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add a failsafe throwing an |
||
// process all subclasses as well | ||
foreach ($class->subClasses as $subClassName) { | ||
$processClasses[] = $this->metadataFactory->getMetadataFor($subClassName); | ||
} | ||
} | ||
|
||
foreach ($possibleEmbeds as $embed) { | ||
if (isset($embeddedDocumentIndexes[$embed])) { | ||
$embeddedIndexes = $embeddedDocumentIndexes[$embed]; | ||
$indexes = []; | ||
$embeddedDocumentIndexes = []; | ||
foreach ($processClasses as $class) { | ||
$visited[$class->name] = true; | ||
|
||
$indexes = array_merge($indexes, $this->prepareIndexes($class)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not mistaken we should be able to work around my first comment here by applying array_unique or calculating some kind of key for an index that's shared between classes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be dirtier than what you're proposing but we're not changing behaviour that was established for years |
||
|
||
// Add indexes from embedded & referenced documents | ||
foreach ($class->fieldMappings as $fieldMapping) { | ||
if (isset($fieldMapping['embedded'])) { | ||
if (isset($fieldMapping['targetDocument'])) { | ||
$possibleEmbeds = [$fieldMapping['targetDocument']]; | ||
} elseif (isset($fieldMapping['discriminatorMap'])) { | ||
$possibleEmbeds = array_unique($fieldMapping['discriminatorMap']); | ||
} else { | ||
$embeddedIndexes = $this->doGetDocumentIndexes($embed, $visited); | ||
$embeddedDocumentIndexes[$embed] = $embeddedIndexes; | ||
continue; | ||
} | ||
|
||
foreach ($embeddedIndexes as $embeddedIndex) { | ||
foreach ($embeddedIndex['keys'] as $key => $value) { | ||
$embeddedIndex['keys'][$fieldMapping['name'] . '.' . $key] = $value; | ||
unset($embeddedIndex['keys'][$key]); | ||
foreach ($possibleEmbeds as $embed) { | ||
if (isset($embeddedDocumentIndexes[$embed])) { | ||
$embeddedIndexes = $embeddedDocumentIndexes[$embed]; | ||
} else { | ||
$embeddedIndexes = $this->doGetDocumentIndexes($embed, $visited); | ||
$embeddedDocumentIndexes[$embed] = $embeddedIndexes; | ||
} | ||
|
||
if (isset($embeddedIndex['options']['name'])) { | ||
$embeddedIndex['options']['name'] = sprintf('%s_%s', $fieldMapping['name'], $embeddedIndex['options']['name']); | ||
} | ||
foreach ($embeddedIndexes as $embeddedIndex) { | ||
foreach ($embeddedIndex['keys'] as $key => $value) { | ||
$embeddedIndex['keys'][$fieldMapping['name'] . '.' . $key] = $value; | ||
unset($embeddedIndex['keys'][$key]); | ||
} | ||
|
||
$indexes[] = $embeddedIndex; | ||
if (isset($embeddedIndex['options']['name'])) { | ||
$embeddedIndex['options']['name'] = sprintf('%s_%s', $fieldMapping['name'], $embeddedIndex['options']['name']); | ||
} | ||
|
||
$indexes[] = $embeddedIndex; | ||
} | ||
} | ||
} | ||
} elseif (isset($fieldMapping['reference']) && isset($fieldMapping['targetDocument'])) { | ||
foreach ($indexes as $idx => $index) { | ||
$newKeys = []; | ||
foreach ($index['keys'] as $key => $v) { | ||
if ($key === $fieldMapping['name']) { | ||
$key = ClassMetadata::getReferenceFieldName($fieldMapping['storeAs'], $key); | ||
} elseif (isset($fieldMapping['reference']) && isset($fieldMapping['targetDocument'])) { | ||
foreach ($indexes as $idx => $index) { | ||
$newKeys = []; | ||
foreach ($index['keys'] as $key => $v) { | ||
if ($key === $fieldMapping['name']) { | ||
$key = ClassMetadata::getReferenceFieldName($fieldMapping['storeAs'], $key); | ||
} | ||
|
||
$newKeys[$key] = $v; | ||
} | ||
|
||
$newKeys[$key] = $v; | ||
$indexes[$idx]['keys'] = $newKeys; | ||
} | ||
|
||
$indexes[$idx]['keys'] = $newKeys; | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ class Project | |
|
||
/** | ||
* @ODM\Field(type="string") | ||
* @ODM\UniqueIndex | ||
* | ||
* @var string|null | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't do such a thing as it changes produced
ClassMetadata
too much:, especially for a patch release. I'm not sure yet if the change would be OK in a minor patch anyway.