/** * 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); } } }
/** * 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()) { $update = $this->pb->prepareUpdateData($document); $query = $this->getQueryForDocument($document); foreach (array_keys($query) as $field) { unset($update['$set'][$field]); } if (empty($update['$set'])) { unset($update['$set']); } // 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); } } if (!empty($update)) { // 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); } } $options = $this->getWriteOptions($options); $result = $this->collection->update($query, $update, $options); if (($this->class->isVersioned || $this->class->isLockable) && !$result['n']) { throw LockException::lockFailed($document); } } $this->handleCollections($document, $options); }