示例#1
0
 /**
  * Commits the UnitOfWork
  *
  * @param object $document
  *
  * @return void
  */
 public function commit($document = null)
 {
     $this->detectChangedDocuments($document);
     if ($this->evm->hasListeners(Event::onFlush)) {
         $this->evm->dispatchEvent(Event::onFlush, new OnFlushEventArgs($this->dm));
     }
     try {
         $utx = $this->session->getWorkspace()->getTransactionManager();
         if ($utx->inTransaction()) {
             $utx = null;
         } else {
             $utx->begin();
         }
     } catch (UnsupportedRepositoryOperationException $e) {
         $utx = null;
     }
     try {
         $this->executeInserts($this->scheduledInserts);
         $this->executeUpdates($this->scheduledUpdates);
         $this->executeUpdates($this->scheduledAssociationUpdates, false);
         $this->executeRemovals($this->scheduledRemovals);
         $this->executeMoves($this->scheduledMoves);
         $this->session->save();
         if ($utx) {
             $utx->commit();
         }
     } catch (\Exception $e) {
         try {
             $this->dm->close();
             if ($utx) {
                 $utx->rollback();
             }
         } catch (\Exception $innerException) {
             //TODO: log error while closing dm after error: $innerException->getMessage
         }
         throw $e;
     }
     foreach ($this->visitedCollections as $col) {
         $col->takeSnapshot();
     }
     foreach ($this->multivaluePropertyCollections as $col) {
         $col->takeSnapshot();
     }
     $this->documentTranslations = $this->scheduledUpdates = $this->scheduledAssociationUpdates = $this->scheduledRemovals = $this->scheduledMoves = $this->scheduledInserts = $this->visitedCollections = array();
 }
示例#2
0
 /**
  * Commits the UnitOfWork
  *
  * @param object|array|null $document optionally limit to a specific
  *                                    document or an array of documents
  */
 public function commit($document = null)
 {
     $this->invokeGlobalEvent(Event::preFlush, new ManagerEventArgs($this->dm));
     if ($document === null) {
         $this->computeChangeSets();
     } elseif (is_object($document)) {
         $this->computeSingleDocumentChangeSet($document);
     } elseif (is_array($document)) {
         foreach ($document as $object) {
             $this->computeSingleDocumentChangeSet($object);
         }
     }
     $this->invokeGlobalEvent(Event::onFlush, new ManagerEventArgs($this->dm));
     if (empty($this->scheduledInserts) && empty($this->scheduledUpdates) && empty($this->scheduledRemovals) && empty($this->scheduledReorders) && empty($this->documentTranslations) && empty($this->scheduledMoves)) {
         $this->invokeGlobalEvent(Event::postFlush, new ManagerEventArgs($this->dm));
         $this->changesetComputed = array();
         // @deprecated This is to maintain BC with the old behavior, where users may call
         //             flush instead of PHPCR\SessionInterface#save
         $this->session->save();
         return;
         // Nothing to do.
     }
     try {
         $utx = $this->session->getWorkspace()->getTransactionManager();
         if ($utx->inTransaction()) {
             $utx = null;
         } else {
             $utx->begin();
         }
     } catch (UnsupportedRepositoryOperationException $e) {
         $utx = null;
     }
     try {
         $this->executeInserts($this->scheduledInserts);
         $this->executeUpdates($this->scheduledUpdates);
         $this->executeRemovals($this->scheduledRemovals);
         $this->executeReorders($this->scheduledReorders);
         $this->executeMoves($this->scheduledMoves);
         $this->session->save();
         if ($utx) {
             $utx->commit();
         }
     } catch (\Exception $e) {
         try {
             $this->dm->close();
             if ($utx) {
                 $utx->rollback();
             }
         } catch (\Exception $innerException) {
             //TODO: log error while closing dm after error: $innerException->getMessage
         }
         throw $e;
     }
     foreach ($this->visitedCollections as $col) {
         $col->takeSnapshot();
     }
     $this->invokeGlobalEvent(Event::postFlush, new ManagerEventArgs($this->dm));
     if (null === $document) {
         foreach ($this->documentLocales as $oid => $locales) {
             $this->documentLocales[$oid]['original'] = $locales['current'];
         }
     } else {
         $documents = is_array($document) ? $document : array($document);
         foreach ($documents as $doc) {
             $oid = spl_object_hash($doc);
             if (isset($this->documentLocales[$oid])) {
                 $this->documentLocales[$oid]['original'] = $this->documentLocales[$oid]['current'];
             }
         }
     }
     $this->scheduledUpdates = $this->scheduledRemovals = $this->scheduledMoves = $this->scheduledReorders = $this->scheduledInserts = $this->visitedCollections = $this->documentChangesets = $this->changesetComputed = array();
     $this->invokeGlobalEvent(Event::endFlush, new ManagerEventArgs($this->dm));
 }