Doctrine\ODM\MongoDB\UnitOfWork::commit PHP Method

commit() public method

The operations are executed in the following order: 1) All document insertions 2) All document updates 3) All document deletions
public commit ( object $document = null, array $options = [] )
$document object
$options array Array of options to be used with batchInsert(), update() and remove()
    public function commit($document = null, array $options = array())
    {
        // Raise preFlush
        if ($this->evm->hasListeners(Events::preFlush)) {
            $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm));
        }
        // Compute changes done since last commit.
        if ($document === null) {
            $this->computeChangeSets();
        } elseif (is_object($document)) {
            $this->computeSingleDocumentChangeSet($document);
        } elseif (is_array($document)) {
            foreach ($document as $object) {
                $this->computeSingleDocumentChangeSet($object);
            }
        }
        if (!($this->documentInsertions || $this->documentUpserts || $this->documentDeletions || $this->documentUpdates || $this->collectionUpdates || $this->collectionDeletions || $this->orphanRemovals)) {
            return;
            // Nothing to do.
        }
        if ($this->orphanRemovals) {
            foreach ($this->orphanRemovals as $removal) {
                $this->remove($removal);
            }
        }
        // Raise onFlush
        if ($this->evm->hasListeners(Events::onFlush)) {
            $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm));
        }
        foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) {
            list($class, $documents) = $classAndDocuments;
            $this->executeUpserts($class, $documents, $options);
        }
        foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) {
            list($class, $documents) = $classAndDocuments;
            $this->executeInserts($class, $documents, $options);
        }
        foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) {
            list($class, $documents) = $classAndDocuments;
            $this->executeUpdates($class, $documents, $options);
        }
        foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) {
            list($class, $documents) = $classAndDocuments;
            $this->executeDeletions($class, $documents, $options);
        }
        // Raise postFlush
        if ($this->evm->hasListeners(Events::postFlush)) {
            $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm));
        }
        // Clear up
        $this->documentInsertions = $this->documentUpserts = $this->documentUpdates = $this->documentDeletions = $this->documentChangeSets = $this->collectionUpdates = $this->collectionDeletions = $this->visitedCollections = $this->scheduledForDirtyCheck = $this->orphanRemovals = $this->hasScheduledCollections = array();
    }

Usage Example

示例#1
0
 /**
  * Flushes all changes to objects that have been queued up to now to the database.
  * This effectively synchronizes the in-memory state of managed objects with the
  * database.
  *
  * @param object $document
  * @param array $options Array of options to be used with batchInsert(), update() and remove()
  */
 public function flush($document = null, array $options = array())
 {
     if (null !== $document && !is_object($document) && !is_array($document)) {
         throw new \InvalidArgumentException(gettype($document));
     }
     $this->errorIfClosed();
     $this->unitOfWork->commit($document, $options);
 }
All Usage Examples Of Doctrine\ODM\MongoDB\UnitOfWork::commit
UnitOfWork