/**
  * @param OnFlushEventArgs $args
  */
 public function onFlush(OnFlushEventArgs $args)
 {
     /* @var $em Doctrine\ORM\EntityManager */
     $em = $args->getEntityManager();
     /* @var $uow Doctrine\ORM\UnitOfWork */
     $uow = $em->getUnitOfWork();
     /* @var $resourceClass Doctrine\ORM\Mapping\ClassMetadata */
     $resourceClass = $em->getClassMetadata('DoctrineExtensions\\Versionable\\Entity\\ResourceVersion');
     foreach ($uow->getScheduledEntityUpdates() as $entity) {
         if ($entity instanceof Versionable) {
             $entityClass = $em->getClassMetadata(get_class($entity));
             if (!$entityClass->isVersioned) {
                 throw Exception::versionedEntityRequired();
             }
             $entityId = $entityClass->getIdentifierValues($entity);
             if (count($entityId) == 1 && current($entityId)) {
                 $entityId = current($entityId);
             } else {
                 throw Exception::singleIdentifierRequired();
             }
             $oldValues = array_map(function ($changeSetField) {
                 return $changeSetField[0];
             }, $uow->getEntityChangeSet($entity));
             $entityVersion = $entityClass->reflFields[$entityClass->versionField]->getValue($entity);
             unset($oldValues[$entityClass->versionField]);
             unset($oldValues[$entityClass->getSingleIdentifierFieldName()]);
             $resourceVersion = new ResourceVersion($entityClass->name, $entityId, $oldValues, $entityVersion);
             $em->persist($resourceVersion);
             $uow->computeChangeSet($resourceClass, $resourceVersion);
         }
     }
 }
 /**
  * @param Versionable $resource
  * @param int $toVersionNum
  */
 public function revert(Versionable $resource, $toVersionNum)
 {
     $versions = $this->getVersions($resource);
     if (!isset($versions[$toVersionNum])) {
         throw Exception::unknownVersion($toVersionNum);
     }
     /* @var $version Entity\ResourceVersion */
     $version = $versions[$toVersionNum];
     $versionableClass = $this->_em->getClassMetadata(get_class($resource));
     foreach ($version->getVersionedData() as $k => $v) {
         $versionableClass->reflFields[$k]->setValue($resource, $v);
     }
     if ($versionableClass->changeTrackingPolicy == ClassMetadata::CHANGETRACKING_DEFERRED_EXPLICIT) {
         $this->_em->persist($resource);
     }
 }