From 321a6a4beab3362e72f363d92b48ddcb7dd39532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Fri, 6 Jun 2025 13:53:42 +0200 Subject: [PATCH] Retrieve ordered results --- src/Controller/FindingController.php | 2 +- src/Controller/NotConservedController.php | 2 +- src/Controller/PrehistoricController.php | 2 +- src/Controller/SiteController.php | 2 +- src/Repository/BibliographyRepository.php | 86 ++++++++++++++--------- src/Repository/DocumentRepository.php | 54 ++++++++------ 6 files changed, 91 insertions(+), 57 deletions(-) diff --git a/src/Controller/FindingController.php b/src/Controller/FindingController.php index 7360bee..e56b2ed 100644 --- a/src/Controller/FindingController.php +++ b/src/Controller/FindingController.php @@ -17,7 +17,7 @@ class FindingController extends AbstractController { $repo = $em->getRepository(Finding::class); - $findings = $repo->findAll(); + $findings = $repo->findBy([], ['label' => 'ASC']); foreach($findings as $key => $finding) { $coords = $repo->coordinates($finding->getId()); diff --git a/src/Controller/NotConservedController.php b/src/Controller/NotConservedController.php index 9db20f1..71518b8 100644 --- a/src/Controller/NotConservedController.php +++ b/src/Controller/NotConservedController.php @@ -20,7 +20,7 @@ class NotConservedController extends AbstractController //$repoBib = $em->getRepository(Bibliography::class); $repoImg = $em->getRepository(Image::class); - $records = $repo->findBy([], ['id' => 'ASC']); + $records = $repo->findBy([], ['label' => 'ASC']); // Terrible? N+1.. foreach ($records as $key => $record) { diff --git a/src/Controller/PrehistoricController.php b/src/Controller/PrehistoricController.php index 25fb1cb..3f6727e 100644 --- a/src/Controller/PrehistoricController.php +++ b/src/Controller/PrehistoricController.php @@ -18,7 +18,7 @@ class PrehistoricController extends AbstractController $repo = $em->getRepository(Prehistoric::class); //$repoBib = $em->getRepository(Bibliography::class); - $records = $repo->findBy([], ['id' => 'ASC']); + $records = $repo->findBy([], ['label' => 'ASC']); // Terrible? N+1.. foreach ($records as $key => $record) { diff --git a/src/Controller/SiteController.php b/src/Controller/SiteController.php index 488ab08..f430412 100644 --- a/src/Controller/SiteController.php +++ b/src/Controller/SiteController.php @@ -20,7 +20,7 @@ class SiteController extends AbstractController { $repo = $em->getRepository(Site::class); - $sites = $repo->findAll(); + $sites = $repo->findBy([], ['label' => 'ASC']); // TODO N+1... foreach($sites as $key => $site) { diff --git a/src/Repository/BibliographyRepository.php b/src/Repository/BibliographyRepository.php index a0c81ef..736cd24 100644 --- a/src/Repository/BibliographyRepository.php +++ b/src/Repository/BibliographyRepository.php @@ -55,51 +55,71 @@ class BibliographyRepository extends ServiceEntityRepository /** * @return Bibliography[] */ - public function findAllByNotConserved(int $notConserId): array + public function findAllByNotConserved(int $notConservedId): array { - $rsm = new ResultSetMappingBuilder($this->getEntityManager()); - $rsm->addRootEntityFromClassMetadata('App\Entity\Bibliography', 'b'); - $query = $this->getEntityManager()->createNativeQuery( - 'SELECT - id, - citazione, - riferimento, - pagine + $conn = $this->getEntityManager()->getConnection(); + + $sql = ' + SELECT + b.id, + b.citazione, + b.riferimento, + bs.pagine FROM bibliografia b - JOIN bibliografia_non_conser - ON id_bibliografia = b.id - WHERE id_non_conser = :id - ORDER BY ordine ASC', - $rsm - ); + INNER JOIN bibliografia_non_conser bs ON bs.id_bibliografia = b.id + WHERE bs.id_non_conser = :notConservedId + ORDER BY bs.ordine ASC + '; - $query->setParameter('id', $notConserId); + $stmt = $conn->prepare($sql); + $results = $stmt->executeQuery(['notConservedId' => $notConservedId])->fetchAllAssociative(); - return $query->getResult(); + $entities = []; + + foreach ($results as $row) { + $biblio = new Bibliography(); + $biblio->setId($row['id']); + $biblio->setCitation($row['citazione']); + $biblio->setReference($row['riferimento']); + $biblio->setPages($row['pagine']); + $entities[] = $biblio; + } + + return $entities; } /** * @return Bibliography[] */ public function findAllByFinding(int $findingId): array { - $rsm = new ResultSetMappingBuilder($this->getEntityManager()); - $rsm->addRootEntityFromClassMetadata('App\Entity\Bibliography', 'b'); - $query = $this->getEntityManager()->createNativeQuery( - 'SELECT - id, - citazione, - riferimento, - pagine + $conn = $this->getEntityManager()->getConnection(); + + $sql = ' + SELECT + b.id, + b.citazione, + b.riferimento, + bs.pagine FROM bibliografia b - JOIN bibliografia_rinvenim - ON id_bibliografia = b.id - WHERE id_rinvenimento = :id - ORDER BY ordine ASC', - $rsm - ); + INNER JOIN bibliografia_rinvenim bs ON bs.id_bibliografia = b.id + WHERE bs.id_rinvenimento = :findingId + ORDER BY bs.ordine ASC + '; - $query->setParameter('id', $findingId); + $stmt = $conn->prepare($sql); + $results = $stmt->executeQuery(['findingId' => $findingId])->fetchAllAssociative(); - return $query->getResult(); + $entities = []; + + foreach ($results as $row) { + $biblio = new Bibliography(); + $biblio->setId($row['id']); + $biblio->setCitation($row['citazione']); + $biblio->setReference($row['riferimento']); + $biblio->setPages($row['pagine']); + $entities[] = $biblio; + } + + return $entities; } } diff --git a/src/Repository/DocumentRepository.php b/src/Repository/DocumentRepository.php index 3adb1b8..1eff53f 100644 --- a/src/Repository/DocumentRepository.php +++ b/src/Repository/DocumentRepository.php @@ -36,6 +36,7 @@ class DocumentRepository extends ServiceEntityRepository FROM documento d INNER JOIN sito_documento sd ON sd.id_documento = d.id WHERE sd.id_sito = :siteId + ORDER BY d.titolo ASC '; $stmt = $conn->prepare($sql); @@ -63,28 +64,41 @@ class DocumentRepository extends ServiceEntityRepository */ public function findByNotConserved(int $notConservedId): array { - $rsm = new ResultSetMappingBuilder($this->getEntityManager()); - $rsm->addRootEntityFromClassMetadata('App\Entity\Document', 'd'); - $query = $this->getEntityManager()->createNativeQuery( - 'SELECT - id, - titolo, - filename, - descrizione, - autori, - luogo, - tipo - FROM documento d - JOIN non_conserv_documento - ON id_documento = d.id - WHERE id_non_conserv = :id - ', - $rsm - ); + $conn = $this->getEntityManager()->getConnection(); - $query->setParameter('id', $notConservedId); + $sql = ' + SELECT + d.id, + d.titolo, + d.filename, + d.descrizione, + d.autori, + d.tipo, + d.luogo + FROM documento d + INNER JOIN non_conserv_documento sd ON sd.id_documento = d.id + WHERE sd.id_non_conserv = :notConservedId + ORDER BY d.titolo ASC + '; - return $query->getResult(); + $stmt = $conn->prepare($sql); + $results = $stmt->executeQuery(['notConservedId' => $notConservedId])->fetchAllAssociative(); + + $entities = []; + + foreach ($results as $row) { + $document = new Document(); + $document->setId($row['id']); + $document->setTitle($row['titolo']); + $document->setFilename($row['filename']); + $document->setDescription($row['descrizione']); + $document->setConservationPlace($row['luogo']); + $document->setType($row['tipo']); + $document->setAuthors($row['autori']); + $entities[] = $document; + } + + return $entities; } // /** // * @return Document[] Returns an array of Document objects