Checks whether a PersistentCollection is scheduled for update.
public isCollectionScheduledForUpdate ( Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface $coll ) : boolean | ||
$coll | Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface | |
return | boolean |
private function getAtomicCollectionUpdateQuery($document) { $update = array(); $collections = $this->uow->getScheduledCollections($document); $collPersister = $this->uow->getCollectionPersister(); foreach ($collections as $coll) { /* @var $coll PersistentCollection */ $mapping = $coll->getMapping(); if ($mapping['strategy'] !== "atomicSet" && $mapping['strategy'] !== "atomicSetArray") { continue; } if ($this->uow->isCollectionScheduledForUpdate($coll)) { $update = array_merge_recursive($update, $collPersister->prepareSetQuery($coll)); $this->uow->unscheduleCollectionUpdate($coll); /* TODO: * Collection can be set for both deletion and update if * PersistentCollection instance was changed. Since we're dealing * with collection update in one query we won't need the $unset. * Line can be removed once the issue is fixed. */ $this->uow->unscheduleCollectionDeletion($coll); } elseif ($this->uow->isCollectionScheduledForDeletion($coll)) { $update = array_merge_recursive($update, $collPersister->prepareDeleteQuery($coll)); $this->uow->unscheduleCollectionDeletion($coll); } } return $update; }
private function handleCollections($document, $options) { // Collection deletions (deletions of complete collections) foreach ($this->uow->getScheduledCollections($document) as $coll) { if ($this->uow->isCollectionScheduledForDeletion($coll)) { $this->cp->delete($coll, $options); } } // Collection updates (deleteRows, updateRows, insertRows) foreach ($this->uow->getScheduledCollections($document) as $coll) { if ($this->uow->isCollectionScheduledForUpdate($coll)) { $this->cp->update($coll, $options); } } // Take new snapshots from visited collections foreach ($this->uow->getVisitedCollections($document) as $coll) { $coll->takeSnapshot(); } }
/** * Prepares the update query to update a given document object in mongodb. * * @param object $document * @return array $updateData */ public function prepareUpdateData($document) { $class = $this->dm->getClassMetadata(get_class($document)); $changeset = $this->uow->getDocumentChangeSet($document); $updateData = array(); foreach ($changeset as $fieldName => $change) { $mapping = $class->fieldMappings[$fieldName]; // skip non embedded document identifiers if (!$class->isEmbeddedDocument && !empty($mapping['id'])) { continue; } list($old, $new) = $change; // @Inc if ($mapping['type'] === 'increment') { if ($new === null) { if ($mapping['nullable'] === true) { $updateData['$set'][$mapping['name']] = null; } else { $updateData['$unset'][$mapping['name']] = true; } } elseif ($new >= $old) { $updateData['$inc'][$mapping['name']] = $new - $old; } else { $updateData['$inc'][$mapping['name']] = ($old - $new) * -1; } // @Field, @String, @Date, etc. } elseif (!isset($mapping['association'])) { if (isset($new) || $mapping['nullable'] === true) { $updateData['$set'][$mapping['name']] = is_null($new) ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new); } else { $updateData['$unset'][$mapping['name']] = true; } // @EmbedOne } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) { // If we have a new embedded document then lets set the whole thing if ($new && $this->uow->isScheduledForInsert($new)) { $updateData['$set'][$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new); // If we don't have a new value then lets unset the embedded document } elseif (!$new) { $updateData['$unset'][$mapping['name']] = true; // Update existing embedded document } else { $update = $this->prepareUpdateData($new); foreach ($update as $cmd => $values) { foreach ($values as $key => $value) { $updateData[$cmd][$mapping['name'] . '.' . $key] = $value; } } } // @ReferenceMany, @EmbedMany } elseif (isset($mapping['association']) && $mapping['type'] === 'many' && $new) { if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($new)) { $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true); } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($new)) { $updateData['$unset'][$mapping['name']] = true; $this->uow->unscheduleCollectionDeletion($new); } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($old)) { $updateData['$unset'][$mapping['name']] = true; $this->uow->unscheduleCollectionDeletion($old); } elseif ($mapping['association'] === ClassMetadata::EMBED_MANY) { foreach ($new as $key => $embeddedDoc) { if (!$this->uow->isScheduledForInsert($embeddedDoc)) { $update = $this->prepareUpdateData($embeddedDoc); foreach ($update as $cmd => $values) { foreach ($values as $name => $value) { $updateData[$cmd][$mapping['name'] . '.' . $key . '.' . $name] = $value; } } } } } // @ReferenceOne } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isOwningSide']) { if (isset($new) || $mapping['nullable'] === true) { $updateData['$set'][$mapping['name']] = is_null($new) ? null : $this->prepareReferencedDocumentValue($mapping, $new); } else { $updateData['$unset'][$mapping['name']] = true; } } } // collections that aren't dirty but could be subject to update are // excluded from change set, let's go through them now foreach ($this->uow->getScheduledCollections($document) as $coll) { $mapping = $coll->getMapping(); if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($coll)) { $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($coll, true); } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($coll)) { $updateData['$unset'][$mapping['name']] = true; $this->uow->unscheduleCollectionDeletion($coll); } // @ReferenceMany is handled by CollectionPersister } return $updateData; }
/** * Returns the embedded document to be stored in MongoDB. * * The return value will usually be an associative array with string keys * corresponding to field names on the embedded document. An object may be * returned if the document is empty, to ensure that a BSON object will be * stored in lieu of an array. * * @param array $embeddedMapping * @param object $embeddedDocument * @return array|object */ public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDocument) { $embeddedDocumentValue = array(); $class = $this->dm->getClassMetadata(get_class($embeddedDocument)); foreach ($class->fieldMappings as $mapping) { // Skip notSaved fields if (!empty($mapping['notSaved'])) { continue; } // Inline ClassMetadataInfo::getFieldValue() $rawValue = $class->reflFields[$mapping['fieldName']]->getValue($embeddedDocument); $value = null; if ($rawValue !== null) { switch (isset($mapping['association']) ? $mapping['association'] : null) { // @Field, @String, @Date, etc. case null: $value = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue); break; case ClassMetadata::EMBED_ONE: case ClassMetadata::REFERENCE_ONE: $value = $this->prepareAssociatedDocumentValue($mapping, $rawValue); break; case ClassMetadata::EMBED_MANY: case ClassMetadata::REFERENCE_MANY: // Skip PersistentCollections already scheduled for deletion/update if ($rawValue instanceof PersistentCollection && ($this->uow->isCollectionScheduledForDeletion($rawValue) || $this->uow->isCollectionScheduledForUpdate($rawValue))) { break; } $pb = $this; $value = $rawValue->map(function ($v) use($pb, $mapping) { return $pb->prepareAssociatedDocumentValue($mapping, $v); })->toArray(); // Numerical reindexing may be necessary to ensure BSON array storage if (in_array($mapping['strategy'], array('setArray', 'pushAll', 'addToSet'))) { $value = array_values($value); } break; default: throw new \UnexpectedValueException('Unsupported mapping association: ' . $mapping['association']); } } // Omit non-nullable fields that would have a null value if ($value === null && $mapping['nullable'] === false) { continue; } $embeddedDocumentValue[$mapping['name']] = $value; } /* Add a discriminator value if the embedded document is not mapped * explicitly to a targetDocument class. */ if (!isset($embeddedMapping['targetDocument'])) { $discriminatorField = $embeddedMapping['discriminatorField']; $discriminatorValue = isset($embeddedMapping['discriminatorMap']) ? array_search($class->name, $embeddedMapping['discriminatorMap']) : $class->name; /* If the discriminator value was not found in the map, use the full * class name. In the future, it may be preferable to throw an * exception here (perhaps based on some strictness option). * * @see DocumentManager::createDBRef() */ if ($discriminatorValue === false) { $discriminatorValue = $class->name; } $embeddedDocumentValue[$discriminatorField] = $discriminatorValue; } /* If the class has a discriminator (field and value), use it. A child * class that is not defined in the discriminator map may only have a * discriminator field and no value, so default to the full class name. */ if (isset($class->discriminatorField)) { $embeddedDocumentValue[$class->discriminatorField] = isset($class->discriminatorValue) ? $class->discriminatorValue : $class->name; } // Ensure empty embedded documents are stored as BSON objects if (empty($embeddedDocumentValue)) { return (object) $embeddedDocumentValue; } /* @todo Consider always casting the return value to an object, or * building $embeddedDocumentValue as an object instead of an array, to * handle the edge case where all database field names are sequential, * numeric keys. */ return $embeddedDocumentValue; }
private function getAtomicCollectionUpdateQuery($document) { $update = array(); $atomicCollUpdates = array(); $atomicCollDeletes = array(); $collPersister = $this->uow->getCollectionPersister(); /* Collect all atomic collections (top-level and nested) to be included * in the update. */ foreach ($this->uow->getScheduledCollections($document) as $coll) { /* If this is a top-level, atomic collection, its scheduled update * or deletion must be included in the document's update query. */ if ($coll->getOwner() === $document) { $mapping = $coll->getMapping(); if ($mapping['strategy'] !== "atomicSet" && $mapping['strategy'] !== "atomicSetArray") { continue; } if ($this->uow->isCollectionScheduledForUpdate($coll)) { $atomicCollUpdates[spl_object_hash($coll)] = $coll; } elseif ($this->uow->isCollectionScheduledForDeletion($coll)) { $atomicCollDeletes[spl_object_hash($coll)] = $coll; } continue; } /* Otherwise, the collection is nested. Check if its top-most parent * is an atomic collection and include it for updating if so. This * is necessary because the atomic parent may not have directly * changed. */ $parent = $coll->getOwner(); while (null !== ($parentAssoc = $this->uow->getParentAssociation($parent))) { list($mapping, $parent, ) = $parentAssoc; } if (!isset($mapping['association']) || $mapping['association'] !== ClassMetadata::EMBED_MANY || $mapping['strategy'] !== 'atomicSet' && $mapping['strategy'] !== 'atomicSetArray') { continue; } $classMetadata = $this->dm->getClassMetadata(get_class($document)); $parentColl = $classMetadata->getFieldValue($document, $mapping['fieldName']); /* It's possible that the atomic parent was independently scheduled * for deletion. In that case, updating nested data is unnecessary. */ if (!$this->uow->isCollectionScheduledForDeletion($parentColl)) { $atomicCollUpdates[spl_object_hash($parentColl)] = $parentColl; } } foreach ($atomicCollUpdates as $coll) { $update = array_merge_recursive($update, $collPersister->prepareSetQuery($coll)); /* Note: If the collection is only be handled because it's an atomic * parent of a scheduled child, the following calls are NOPs. */ $this->uow->unscheduleCollectionUpdate($coll); } foreach ($atomicCollDeletes as $coll) { $update = array_merge_recursive($update, $collPersister->prepareDeleteQuery($coll)); /* Note: We don't need to call unscheduleCollectionUpdate(), because * the collection should never have been added to $atomicCollDeletes * if it was independently scheduled for update. */ $this->uow->unscheduleCollectionDeletion($coll); } return $update; }