/** * Deletes a PersistentCollection instance completely from a document using $unset. * * @param PersistentCollection $coll * @param array $options */ public function delete(PersistentCollection $coll, array $options) { $mapping = $coll->getMapping(); if ($mapping['isInverseSide']) { return; // ignore inverse side } if (CollectionHelper::isAtomic($mapping['strategy'])) { throw new \UnexpectedValueException($mapping['strategy'] . ' delete collection strategy should have been handled by DocumentPersister. Please report a bug in issue tracker'); } list($propertyPath, $parent) = $this->getPathAndParent($coll); $query = array('$unset' => array($propertyPath => true)); $this->executeQuery($parent, $query, $options); }
/** * Map a field. * * @param array $mapping The mapping information. * * @return array * * @throws MappingException */ public function mapField(array $mapping) { if (!isset($mapping['fieldName']) && isset($mapping['name'])) { $mapping['fieldName'] = $mapping['name']; } if (!isset($mapping['fieldName'])) { throw MappingException::missingFieldName($this->name); } if (!isset($mapping['name'])) { $mapping['name'] = $mapping['fieldName']; } if ($this->identifier === $mapping['name'] && empty($mapping['id'])) { throw MappingException::mustNotChangeIdentifierFieldsType($this->name, $mapping['name']); } if (isset($this->fieldMappings[$mapping['fieldName']])) { //throw MappingException::duplicateFieldMapping($this->name, $mapping['fieldName']); } if ($this->discriminatorField !== null && $this->discriminatorField == $mapping['name']) { throw MappingException::discriminatorFieldConflict($this->name, $this->discriminatorField); } if (isset($mapping['targetDocument']) && strpos($mapping['targetDocument'], '\\') === false && strlen($this->namespace)) { $mapping['targetDocument'] = $this->namespace . '\\' . $mapping['targetDocument']; } if (isset($mapping['discriminatorMap'])) { foreach ($mapping['discriminatorMap'] as $key => $class) { if (strpos($class, '\\') === false && strlen($this->namespace)) { $mapping['discriminatorMap'][$key] = $this->namespace . '\\' . $class; } } } if (isset($mapping['cascade']) && isset($mapping['embedded'])) { throw MappingException::cascadeOnEmbeddedNotAllowed($this->name, $mapping['fieldName']); } $cascades = isset($mapping['cascade']) ? array_map('strtolower', (array) $mapping['cascade']) : array(); if (in_array('all', $cascades) || isset($mapping['embedded'])) { $cascades = array('remove', 'persist', 'refresh', 'merge', 'detach'); } if (isset($mapping['embedded'])) { unset($mapping['cascade']); } elseif (isset($mapping['cascade'])) { $mapping['cascade'] = $cascades; } $mapping['isCascadeRemove'] = in_array('remove', $cascades); $mapping['isCascadePersist'] = in_array('persist', $cascades); $mapping['isCascadeRefresh'] = in_array('refresh', $cascades); $mapping['isCascadeMerge'] = in_array('merge', $cascades); $mapping['isCascadeDetach'] = in_array('detach', $cascades); if (isset($mapping['type']) && $mapping['type'] === 'file') { $mapping['file'] = true; } if (isset($mapping['file']) && $mapping['file'] === true) { $this->file = $mapping['fieldName']; $mapping['name'] = 'file'; } if (isset($mapping['distance']) && $mapping['distance'] === true) { $this->distance = $mapping['fieldName']; } if (isset($mapping['id']) && $mapping['id'] === true) { $mapping['name'] = '_id'; $this->identifier = $mapping['fieldName']; if (isset($mapping['strategy'])) { $this->generatorType = constant('CosmoW\\ODM\\Riak\\Mapping\\ClassMetadata::GENERATOR_TYPE_' . strtoupper($mapping['strategy'])); } $this->generatorOptions = isset($mapping['options']) ? $mapping['options'] : array(); switch ($this->generatorType) { case self::GENERATOR_TYPE_AUTO: $mapping['type'] = 'id'; break; default: if (!empty($this->generatorOptions['type'])) { $mapping['type'] = $this->generatorOptions['type']; } elseif (empty($mapping['type'])) { $mapping['type'] = $this->generatorType === self::GENERATOR_TYPE_INCREMENT ? 'int_id' : 'custom_id'; } } unset($this->generatorOptions['type']); } if (!isset($mapping['nullable'])) { $mapping['nullable'] = false; } if (isset($mapping['reference']) && !empty($mapping['simple']) && !isset($mapping['targetDocument'])) { throw MappingException::simpleReferenceRequiresTargetDocument($this->name, $mapping['fieldName']); } if (isset($mapping['reference']) && empty($mapping['targetDocument']) && empty($mapping['discriminatorMap']) && (isset($mapping['mappedBy']) || isset($mapping['inversedBy']))) { throw MappingException::owningAndInverseReferencesRequireTargetDocument($this->name, $mapping['fieldName']); } if ($this->isEmbeddedDocument && $mapping['type'] === 'many' && CollectionHelper::isAtomic($mapping['strategy'])) { throw MappingException::atomicCollectionStrategyNotAllowed($mapping['strategy'], $this->name, $mapping['fieldName']); } if (isset($mapping['reference']) && $mapping['type'] === 'one') { $mapping['association'] = self::REFERENCE_ONE; } if (isset($mapping['reference']) && $mapping['type'] === 'many') { $mapping['association'] = self::REFERENCE_MANY; } if (isset($mapping['embedded']) && $mapping['type'] === 'one') { $mapping['association'] = self::EMBED_ONE; } if (isset($mapping['embedded']) && $mapping['type'] === 'many') { $mapping['association'] = self::EMBED_MANY; } if (isset($mapping['association']) && !isset($mapping['targetDocument']) && !isset($mapping['discriminatorField'])) { $mapping['discriminatorField'] = self::DEFAULT_DISCRIMINATOR_FIELD; } /* if (isset($mapping['type']) && ($mapping['type'] === 'one' || $mapping['type'] === 'many')) { $mapping['type'] = $mapping['type'] === 'one' ? self::ONE : self::MANY; } */ if (isset($mapping['version'])) { $mapping['notSaved'] = true; $this->setVersionMapping($mapping); } if (isset($mapping['lock'])) { $mapping['notSaved'] = true; $this->setLockMapping($mapping); } $mapping['isOwningSide'] = true; $mapping['isInverseSide'] = false; if (isset($mapping['reference'])) { if (isset($mapping['inversedBy']) && $mapping['inversedBy']) { $mapping['isOwningSide'] = true; $mapping['isInverseSide'] = false; } if (isset($mapping['mappedBy']) && $mapping['mappedBy']) { $mapping['isInverseSide'] = true; $mapping['isOwningSide'] = false; } if (isset($mapping['repositoryMethod'])) { $mapping['isInverseSide'] = true; $mapping['isOwningSide'] = false; } if (!isset($mapping['orphanRemoval'])) { $mapping['orphanRemoval'] = false; } } if (isset($mapping['reference']) && $mapping['type'] === 'many' && $mapping['isOwningSide'] && !empty($mapping['sort']) && !CollectionHelper::usesSet($mapping['strategy'])) { throw MappingException::referenceManySortMustNotBeUsedWithNonSetCollectionStrategy($this->name, $mapping['fieldName'], $mapping['strategy']); } $this->fieldMappings[$mapping['fieldName']] = $mapping; if (isset($mapping['association'])) { $this->associationMappings[$mapping['fieldName']] = $mapping; } return $mapping; }
/** * Marks the PersistentCollection's top-level owner as having a relation to * a collection scheduled for update or deletion. * * If the owner is not scheduled for any lifecycle action, it will be * scheduled for update to ensure that versioning takes place if necessary. * * If the collection is nested within atomic collection, it is immediately * unscheduled and atomic one is scheduled for update instead. This makes * calculating update data way easier. * * @param PersistentCollection $coll */ private function scheduleCollectionOwner(PersistentCollection $coll) { $document = $this->getOwningDocument($coll->getOwner()); $this->hasScheduledCollections[spl_object_hash($document)][spl_object_hash($coll)] = $coll; if ($document !== $coll->getOwner()) { $parent = $coll->getOwner(); while (null !== ($parentAssoc = $this->getParentAssociation($parent))) { list($mapping, $parent, ) = $parentAssoc; } if (isset($mapping['strategy']) && CollectionHelper::isAtomic($mapping['strategy'])) { $class = $this->dm->getClassMetadata(get_class($document)); $atomicCollection = $class->getFieldValue($document, $mapping['fieldName']); $this->scheduleCollectionUpdate($atomicCollection); $this->unscheduleCollectionDeletion($coll); $this->unscheduleCollectionUpdate($coll); } } if (!$this->isDocumentScheduled($document)) { $this->scheduleForUpdate($document); } }
/** * Prepares the update query to upsert a given document object in riak. * * @param object $document * @return array $updateData */ public function prepareUpsertData($document) { $class = $this->dm->getClassMetadata(get_class($document)); $changeset = $this->uow->getDocumentChangeSet($document); $updateData = array(); foreach ($changeset as $fieldName => $change) { $mapping = $class->fieldMappings[$fieldName]; list($old, $new) = $change; // @Inc if ($mapping['type'] === 'increment') { if ($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); } // @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 do nothing on upsert } elseif (!$new) { // Update existing embedded document } else { $update = $this->prepareUpsertData($new); foreach ($update as $cmd => $values) { foreach ($values as $key => $value) { $updateData[$cmd][$mapping['name'] . '.' . $key] = $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); } // @ReferenceMany, @EmbedMany } elseif ($mapping['type'] === ClassMetadata::MANY && !$mapping['isInverseSide'] && $new instanceof PersistentCollection && $new->isDirty() && CollectionHelper::isAtomic($mapping['strategy'])) { $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true); } // @EmbedMany and @ReferenceMany are handled by CollectionPersister } // add discriminator if the class has one if (isset($class->discriminatorField)) { $updateData['$set'][$class->discriminatorField] = isset($class->discriminatorValue) ? $class->discriminatorValue : $class->name; } return $updateData; }