Add repository methods for filtering

This commit is contained in:
2025-12-09 17:07:05 +01:00
parent c1cf5a8bb2
commit 9af4d19744
5 changed files with 406 additions and 8 deletions

View File

@@ -18,12 +18,13 @@ use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\RequestStack;
final readonly class EvaluationRepository implements EvaluationRepositoryInterface
{
private EntityRepository $repository;
public function __construct(private EntityManagerInterface $entityManager)
public function __construct(private EntityManagerInterface $entityManager, private RequestStack $requestStack)
{
$this->repository = $entityManager->getRepository(Evaluation::class);
}
@@ -70,6 +71,11 @@ final readonly class EvaluationRepository implements EvaluationRepositoryInterfa
return Evaluation::class;
}
private function getLang(): string
{
return $this->requestStack->getCurrentRequest()?->getLocale() ?? 'fr';
}
public function getResult(
QueryBuilder $qb,
?int $start = 0,
@@ -93,9 +99,11 @@ final readonly class EvaluationRepository implements EvaluationRepositoryInterfa
{
$qb = $this->entityManager->createQueryBuilder()->from(Evaluation::class, 'e');
$qb->expr()->like('e.title', 'CONCAT(\'%\', LOWER(UNACCENT(:pattern)), \'%\')');
$qb->setParameter('pattern', $pattern);
// Extract the current locale's value from the JSON `title` and search on it
$qb
->where($qb->expr()->like('LOWER(UNACCENT(JSON_EXTRACT(e.title, :lang)))', "CONCAT('%', LOWER(UNACCENT(:pattern)), '%')"))
->setParameter('pattern', $pattern)
->setParameter('lang', $this->getLang());
return $qb;
}