*/ class DoctrineClientRepository extends ServiceEntityRepository implements ClientRepositoryInterface { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Client::class); } public function findById(int $id): ?Client { return $this->find($id); } public function save(Client $client): void { $this->getEntityManager()->persist($client); $this->getEntityManager()->flush(); } public function createListQueryBuilder( bool $includeArchived = false, ?string $search = null, ?string $categoryType = null, ): QueryBuilder { $qb = $this->createQueryBuilder('c') ->andWhere('c.deletedAt IS NULL') ->orderBy('c.companyName', 'ASC') ; if (!$includeArchived) { $qb->andWhere('c.isArchived = false'); } $this->applySearch($qb, $search); $this->applyCategoryType($qb, $categoryType); return $qb; } /** * Recherche fuzzy insensible a la casse sur companyName + lastName + email. * Les metacaracteres LIKE (%, _, \) saisis sont echappes pour rester * litteraux. */ private function applySearch(QueryBuilder $qb, ?string $search): void { if (null === $search || '' === trim($search)) { return; } $escaped = str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], trim($search)); $pattern = '%'.mb_strtolower($escaped, 'UTF-8').'%'; $qb->andWhere( 'LOWER(c.companyName) LIKE :search ' .'OR LOWER(c.lastName) LIKE :search ' .'OR LOWER(c.email) LIKE :search', )->setParameter('search', $pattern); } /** * Restreint aux clients possedant au moins une categorie du type donne. * Sous-requete IN (plutot qu'un JOIN sur la collection M2M) pour ne pas * perturber le DISTINCT / ORDER BY de la requete principale. */ private function applyCategoryType(QueryBuilder $qb, ?string $categoryType): void { if (null === $categoryType || '' === trim($categoryType)) { return; } $sub = $this->getEntityManager()->createQueryBuilder() ->select('c2.id') ->from(Client::class, 'c2') ->join('c2.categories', 'cat2') ->join('cat2.categoryType', 'ct2') ->where('ct2.code = :categoryType') ; $qb->andWhere($qb->expr()->in('c.id', $sub->getDQL())) ->setParameter('categoryType', trim($categoryType)) ; } }