src/Repository/VilleRepository.php line 92

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Ville;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. /**
  7.  * @method Ville|null find($id, $lockMode = null, $lockVersion = null)
  8.  * @method Ville|null findOneBy(array $criteria, array $orderBy = null)
  9.  * @method Ville[]    findAll()
  10.  * @method Ville[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  11.  */
  12. class VilleRepository extends ServiceEntityRepository
  13. {
  14.     public function __construct(ManagerRegistry $registry)
  15.     {
  16.         parent::__construct($registryVille::class);
  17.     }
  18.     /**
  19.      * @param $cp
  20.      * @param $slug
  21.      * @return Ville|null
  22.      * @throws \Doctrine\ORM\NonUniqueResultException
  23.      */
  24.     public function findOneByMultipleFields($cp$slug): ?Ville
  25.     {
  26.         return $this->createQueryBuilder('v')
  27.             ->andWhere('v.codePostal = :cp')
  28.             ->setParameter('cp'$cp)
  29.             ->andWhere('v.slug = :slug OR v.slugComplet = :slug OR v.slugAncienneCommuneLieuDit = :slug')
  30.             ->setParameter('slug'$slug)
  31.             ->setMaxResults(1)
  32.             ->getQuery()
  33.             ->getOneOrNullResult();
  34.     }
  35.     /**
  36.      * @param $cp
  37.      * @param $slug
  38.      * @return Ville|null
  39.      * @throws \Doctrine\ORM\NonUniqueResultException
  40.      */
  41.     public function getVilleByOldSlug($cp$slug): ?Ville
  42.     {
  43.         return $this->createQueryBuilder('v')
  44.             ->andWhere('v.codePostal = :cp')
  45.             ->setParameter('cp'$cp)
  46.             ->andWhere('v.slug LIKE :slug OR v.slugAncienneCommuneLieuDit LIKE :slug')
  47.             ->setParameter('slug''%' $slug '%')
  48.             ->setMaxResults(1)
  49.             ->getQuery()
  50.             ->getOneOrNullResult();
  51.     }
  52.     /**
  53.      * @param $cp
  54.      * @param $slug
  55.      * @return Ville|null
  56.      * @throws \Doctrine\ORM\NonUniqueResultException
  57.      */
  58.     public function getVilleBySlug($code$slug): ?array
  59.     {
  60.         return $this->createQueryBuilder('v')
  61.             ->leftJoin('v.departement','dpt')
  62.             ->andWhere('dpt.code = :code')
  63.             ->setParameter('code'$code)
  64.             ->andWhere('v.slug LIKE :slug OR v.slugAncienneCommuneLieuDit LIKE :slug')
  65.             ->setParameter('slug''%' $slug '%')
  66.             ->getQuery()
  67.             ->getResult();
  68.     }
  69.     /**
  70.      * @return Ville|null
  71.      * @throws \Doctrine\ORM\NonUniqueResultException
  72.      */
  73.     public function getAllVillesByInsee()
  74.     {
  75.         return $this->createQueryBuilder('v')
  76.             ->where('v.insee IS NOT NULL')
  77.             ->getQuery()
  78.             ->getResult();
  79.     }
  80.     /**
  81.      * @return Ville|null
  82.      * @throws \Doctrine\ORM\NonUniqueResultException
  83.      */
  84.     public function getVilleByCodeInsee($insee null)
  85.     {
  86.         $qb $this->createQueryBuilder('v');
  87.         if ($insee) {
  88.             $qb->where('v.codeInsee = :codeInsee')
  89.                 ->setParameter('codeInsee'$insee);
  90.         }
  91.         return $qb->getQuery()->getOneOrNullResult();
  92.     }
  93. }