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();
}