示例#1
0
 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();
     }
 }
示例#2
0
 /**
  * Prepares the update query to update a given document object in riak.
  *
  * @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;
 }