Doctrine\ODM\MongoDB\Persisters\PersistenceBuilder::prepareUpdateData PHP Method

prepareUpdateData() public method

Prepares update array for document, using atomic operators
public prepareUpdateData ( mixed $document ) : array
$document mixed
return array
    public function prepareUpdateData($document)
    {
        $oid = spl_object_hash($document);
        $class = $this->dm->getClassMetadata(get_class($document));
        $changeset = $this->uow->getDocumentChangeSet($document);
        $result = array();
        foreach ($changeset as $fieldName => $change) {
            $mapping = $class->fieldMappings[$fieldName];
            // many references are persisted with CollectionPersister later
            if (isset($mapping['reference']) && $mapping['type'] === 'many') {
                continue;
            }
            // skip not saved fields
            if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) {
                continue;
            }
            // Skip version and lock fields
            if (isset($mapping['version']) || isset($mapping['lock'])) {
                continue;
            }

            list($old, $new) = $change;

            // Build query to persist updates to an embedded one association
            if (isset($mapping['embedded']) && $mapping['type'] === 'one') {
                // If we have a new embedded document then lets set the whole thing
                if ($new && $this->uow->isScheduledForInsert($new)) {
                    $result[$this->cmd . 'set'][$mapping['name']] = $this->prepareEmbeddedDocValue($mapping, $new);
                // If we don't have a new value then lets unset the embedded document
                } else if ( ! $new) {
                    $result[$this->cmd . 'unset'][$mapping['name']] = true;
                // Update existing embedded document
                } else {
                    $update = $this->prepareUpdateData($new);
                    foreach ($update as $cmd => $values) {
                        foreach ($values as $key => $value) {
                            $result[$cmd][$mapping['name'] . '.' . $key] = $value;
                        }
                    }
                }
            // Build query to persist updates to an embedded many association
            } else if (isset($mapping['embedded']) && $mapping['type'] === '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) {
                                $result[$cmd][$mapping['name'] . '.' . $key . '.' . $name] = $value;
                            }
                        }
                    }
                }
            // Prepare increment type values
            } else if ($mapping['type'] === 'increment') {
                if ($new >= $old) {
                    $result[$this->cmd . 'inc'][$mapping['name']] = $new - $old;
                } else {
                    $result[$this->cmd . 'inc'][$mapping['name']] = ($old - $new) * -1;
                }
            // Persist all other types using $set and $unset
            } else {
                if (isset($new) || $mapping['nullable'] === true) {
                    $new = $this->prepareValue($mapping, $new);
                    $result[$this->cmd . 'set'][$mapping['name']] = $new;
                } else {
                    $result[$this->cmd . 'unset'][$mapping['name']] = true;
                }
            }
        }
        return $result;
    }

Usage Example

コード例 #1
0
 /**
  * Updates the already persisted document if it has any new changesets.
  *
  * @param object $document
  * @param array $options Array of options to be used with update()
  * @throws \Doctrine\ODM\MongoDB\LockException
  */
 public function update($document, array $options = array())
 {
     $id = $this->uow->getDocumentIdentifier($document);
     $update = $this->pb->prepareUpdateData($document);
     if (!empty($update) || $this->uow->hasScheduledCollections($document)) {
         $id = $this->class->getDatabaseIdentifierValue($id);
         $query = array('_id' => $id);
         // Include versioning logic to set the new version value in the database
         // and to ensure the version has not changed since this document object instance
         // was fetched from the database
         if ($this->class->isVersioned) {
             $versionMapping = $this->class->fieldMappings[$this->class->versionField];
             $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document);
             if ($versionMapping['type'] === 'int') {
                 $nextVersion = $currentVersion + 1;
                 $update['$inc'][$versionMapping['name']] = 1;
                 $query[$versionMapping['name']] = $currentVersion;
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
             } elseif ($versionMapping['type'] === 'date') {
                 $nextVersion = new \DateTime();
                 $update['$set'][$versionMapping['name']] = new \MongoDate($nextVersion->getTimestamp());
                 $query[$versionMapping['name']] = new \MongoDate($currentVersion->getTimestamp());
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
             }
         }
         $atomicCollectionQuery = $this->getAtomicCollectionUpdateQuery($document);
         if (!empty($atomicCollectionQuery)) {
             $update = array_merge_recursive($update, $atomicCollectionQuery);
         }
         /* We got here because the document has one or more related
          * PersistentCollections to be committed later; however, if the
          * document is not versioned then there is nothing left to do.
          */
         if (empty($update)) {
             return;
         }
         // Include locking logic so that if the document object in memory is currently
         // locked then it will remove it, otherwise it ensures the document is not locked.
         if ($this->class->isLockable) {
             $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document);
             $lockMapping = $this->class->fieldMappings[$this->class->lockField];
             if ($isLocked) {
                 $update['$unset'] = array($lockMapping['name'] => true);
             } else {
                 $query[$lockMapping['name']] = array('$exists' => false);
             }
         }
         unset($update['$set']['_id']);
         $result = $this->collection->update($query, $update, $options);
         if (($this->class->isVersioned || $this->class->isLockable) && !$result['n']) {
             throw LockException::lockFailed($document);
         }
     }
 }
All Usage Examples Of Doctrine\ODM\MongoDB\Persisters\PersistenceBuilder::prepareUpdateData