Neos\Flow\Persistence\Doctrine\PersistenceManager::persistAll PHP Method

persistAll() public method

Commits new objects and changes to objects in the current persistence session into the backend
public persistAll ( boolean $onlyWhitelistedObjects = false ) : void
$onlyWhitelistedObjects boolean If TRUE an exception will be thrown if there are scheduled updates/deletes or insertions for objects that are not "whitelisted" (see AbstractPersistenceManager::whitelistObject())
return void
    public function persistAll($onlyWhitelistedObjects = false)
    {
        if ($onlyWhitelistedObjects) {
            $unitOfWork = $this->entityManager->getUnitOfWork();
            /** @var \Doctrine\ORM\UnitOfWork $unitOfWork */
            $unitOfWork->computeChangeSets();
            $objectsToBePersisted = $unitOfWork->getScheduledEntityUpdates() + $unitOfWork->getScheduledEntityDeletions() + $unitOfWork->getScheduledEntityInsertions();
            foreach ($objectsToBePersisted as $object) {
                $this->throwExceptionIfObjectIsNotWhitelisted($object);
            }
        }
        if (!$this->entityManager->isOpen()) {
            $this->systemLogger->log('persistAll() skipped flushing data, the Doctrine EntityManager is closed. Check the logs for error message.', LOG_ERR);
            return;
        }
        try {
            $this->entityManager->flush();
        } catch (Exception $exception) {
            $this->systemLogger->logException($exception);
            /** @var Connection $connection */
            $connection = $this->entityManager->getConnection();
            $connection->close();
            $connection->connect();
            $this->systemLogger->log('Reconnected the Doctrine EntityManager to the persistence backend.', LOG_INFO);
            $this->entityManager->flush();
        } finally {
            $this->emitAllObjectsPersisted();
        }
    }

Usage Example

 /**
  * @test
  * @expectedException \Neos\Flow\Error\Exception
  */
 public function persistAllReconnectsConnectionOnFailure()
 {
     $this->mockEntityManager->expects($this->exactly(2))->method('flush')->will($this->throwException(new FlowError\Exception('Dummy connection error')));
     $this->mockConnection->expects($this->at(0))->method('close');
     $this->mockConnection->expects($this->at(1))->method('connect');
     $this->persistenceManager->persistAll();
 }