Test relations for bibliography

This commit is contained in:
Nicolò P 2024-11-04 22:08:44 +01:00
parent 0a41ff4a7b
commit a2f518ee15
5 changed files with 33 additions and 7 deletions

View File

@ -3,6 +3,7 @@
namespace App\Controller;
use App\Entity\Bibliography;
use App\Entity\Collection;
use App\Form\BibliographyType;
//use App\Security\Voter\VocabVoter;
use Doctrine\ORM\EntityManagerInterface;
@ -17,6 +18,11 @@ class BibliographyController extends AbstractController
#[Route('/bibliography/{id<\d+>}', name: 'app_bibliography')]
public function index(Bibliography $bibliography, EntityManagerInterface $em): Response
{
$repo = $em->getRepository(Collection::class);
$collections = $repo->findAllByBibliography($bibliography->getId());
$bibliography->setCollections($collections);
return $this->render('bibliography/index.html.twig', [
'controller_name' => 'BibliographyController',
'record' => $bibliography,

View File

@ -14,7 +14,8 @@ class CollectionController extends AbstractController
#[Route('/collection/{id<\d+>}', name: 'app_collection')]
public function index(Collection $collection, EntityManagerInterface $em): Response
{
$bibliographies = $em->getRepository(Bibliography::class)->findAllCollection($collection->getId());
$repo = $em->getRepository(Bibliography::class);
$bibliographies = $repo->findAllByCollection($collection->getId());
$collection->setBibliographies($bibliographies);

View File

@ -2,15 +2,14 @@
namespace App\Entity;
//use App\Repository\UserRepository;
use App\Repository\CollectionRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\DBAL\Types\Types;
use App\RecordStatus;
use Doctrine\Common\Collections\Collection as DoctrineCollection;
#[ORM\Entity()]
#[ORM\Entity(repositoryClass: CollectionRepository::class)]
#[ORM\Table(name: 'collection')]
class Collection
{

View File

@ -18,7 +18,7 @@ class BibliographyRepository extends ServiceEntityRepository
parent::__construct($registry, Bibliography::class);
}
public function findAllCollection(int $collectionId): ?ArrayCollection
public function findAllByCollection(int $collectionId): ?ArrayCollection
{
$rsm = new ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata('App\Entity\Bibliography', 'b');

View File

@ -3,9 +3,10 @@
namespace App\Repository;
use App\Entity\Collection;
use App\Repository\BibliographyRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
/**
* @extends ServiceEntityRepository<Collection>
@ -16,5 +17,24 @@ class CollectionRepository extends ServiceEntityRepository
{
parent::__construct($registry, Collection::class);
}
}
public function findAllByBibliography(int $biblioId): ?ArrayCollection
{
$rsm = new ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata('App\Entity\Collection', 'c');
$query = $this->getEntityManager()->createNativeQuery(
"SELECT id, stato, editor, tit_coll, data_coll
FROM collection c
JOIN rel_riferimento_collezione
ON Collezione_id_coll = id
WHERE Bibliografia_id_bib = :biblioId",
$rsm
);
$query->setParameter('biblioId', $biblioId);
$collections = new ArrayCollection($query->getResult());
return $collections;
}
}