diff --git a/src/Service/SnapshotService.php b/src/Service/SnapshotService.php index 44cafdd5..d30ecbe4 100755 --- a/src/Service/SnapshotService.php +++ b/src/Service/SnapshotService.php @@ -75,7 +75,7 @@ public function create(Anr $anr, array $data): Snapshot public function delete(Anr $anr, int $id): void { /** @var Snapshot $snapshot */ - $snapshot = $this->snapshotTable->findByIdAndAnr($id, $anr); + $snapshot = $this->snapshotTable->findByIdAndAnrReference($id, $anr); $this->snapshotTable->remove($snapshot); } diff --git a/src/Table/SnapshotTable.php b/src/Table/SnapshotTable.php index 40bc2051..2c7d8734 100755 --- a/src/Table/SnapshotTable.php +++ b/src/Table/SnapshotTable.php @@ -8,6 +8,7 @@ namespace Monarc\FrontOffice\Table; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityNotFoundException; use Monarc\Core\Table\AbstractTable; use Monarc\FrontOffice\Entity\Anr; use Monarc\FrontOffice\Entity\Snapshot; @@ -42,4 +43,20 @@ public function findByAnrReferenceAndOrderBy(Anr $anr, array $order): array return $queryBuilder->getQuery()->getResult(); } + + public function findByIdAndAnrReference(int $id, Anr $anr): Snapshot + { + $snapshot = $this->getRepository()->createQueryBuilder('s') + ->where('s.id = :id') + ->andWhere('s.anrReference = :anr') + ->setParameter('id', $id) + ->setParameter('anr', $anr) + ->getQuery() + ->getOneOrNullResult(); + if ($snapshot === null) { + throw EntityNotFoundException::fromClassNameAndIdentifier(SnapshotTable::class, [$id, $anr->getId()]); + } + + return $snapshot; + } }