Skip to content

Commit

Permalink
Catch MongoDBExceptions to throw ModelManagerExceptions (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 authored Apr 1, 2023
1 parent 77f01d8 commit 2f58aaa
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Iterator\Iterator;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MongoDBClassMetadata;
use Doctrine\ODM\MongoDB\MongoDBException;
use Doctrine\ODM\MongoDB\Query\Builder;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\Persistence\Mapping\MappingException;
use MongoDB\Driver\Exception\Exception;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface as BaseProxyQueryInterface;
use Sonata\AdminBundle\Exception\ModelManagerException;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Model\ProxyResolverInterface;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
Expand All @@ -44,23 +47,47 @@ public function __construct(

public function create(object $object): void
{
$documentManager = $this->getDocumentManager($object);
$documentManager->persist($object);
$documentManager->flush();
try {
$documentManager = $this->getDocumentManager($object);
$documentManager->persist($object);
$documentManager->flush();
} catch (Exception|MongoDBException $exception) {
throw new ModelManagerException(
sprintf('Failed to create object: %s', $this->getRealClass($object)),
$exception->getCode(),
$exception
);
}
}

public function update(object $object): void
{
$documentManager = $this->getDocumentManager($object);
$documentManager->persist($object);
$documentManager->flush();
try {
$documentManager = $this->getDocumentManager($object);
$documentManager->persist($object);
$documentManager->flush();
} catch (Exception|MongoDBException $exception) {
throw new ModelManagerException(
sprintf('Failed to update object: %s', $this->getRealClass($object)),
$exception->getCode(),
$exception
);
}
}

public function delete(object $object): void
{
$documentManager = $this->getDocumentManager($object);
$documentManager->remove($object);
$documentManager->flush();
try {
$documentManager = $this->getDocumentManager($object);
$documentManager->remove($object);
$documentManager->flush();
} catch (Exception|MongoDBException $exception) {
throw new ModelManagerException(
sprintf('Failed to delete object: %s', $this->getRealClass($object)),
$exception->getCode(),
$exception
);
}
}

/**
Expand Down Expand Up @@ -207,17 +234,26 @@ public function batchDelete(string $class, BaseProxyQueryInterface $query): void
\assert($iterator instanceof Iterator);

$i = 0;
foreach ($iterator as $object) {
$documentManager->remove($object);

if (0 === (++$i % 20)) {
$documentManager->flush();
$documentManager->clear();
try {
foreach ($iterator as $object) {
$documentManager->remove($object);

if (0 === (++$i % 20)) {
$documentManager->flush();
$documentManager->clear();
}
}
}

$documentManager->flush();
$documentManager->clear();
$documentManager->flush();
$documentManager->clear();
} catch (Exception|MongoDBException $exception) {
throw new ModelManagerException(
sprintf('Failed to delete object: %s', $class),
$exception->getCode(),
$exception
);
}
}

public function getExportFields(string $class): array
Expand Down

0 comments on commit 2f58aaa

Please sign in to comment.