Checks whether the document is related to a PersistentCollection
scheduled for update or deletion.
public hasScheduledCollections ( object $document ) : boolean | ||
$document | object | |
Результат | boolean |
/** * 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); } } }
/** * Cascades the postUpdate and postPersist events to embedded documents. * * @param ClassMetadata $class * @param object $document */ private function cascadePostUpdate(ClassMetadata $class, $document) { foreach ($class->getEmbeddedFieldsMappings() as $mapping) { $value = $class->reflFields[$mapping['fieldName']]->getValue($document); if ($value === null) { continue; } $values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value; foreach ($values as $entry) { if (empty($this->uow->getDocumentChangeSet($entry)) && !$this->uow->hasScheduledCollections($entry)) { continue; } $entryClass = $this->dm->getClassMetadata(get_class($entry)); $event = $this->uow->isScheduledForInsert($entry) ? Events::postPersist : Events::postUpdate; $entryClass->invokeLifecycleCallbacks($event, $entry, array(new LifecycleEventArgs($entry, $this->dm))); $this->evm->dispatchEvent($event, new LifecycleEventArgs($entry, $this->dm)); $this->cascadePostUpdate($entryClass, $entry); } } }