<?php
namespace App\Repository;
use App\Entity\Ville;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Ville|null find($id, $lockMode = null, $lockVersion = null)
* @method Ville|null findOneBy(array $criteria, array $orderBy = null)
* @method Ville[] findAll()
* @method Ville[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class VilleRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Ville::class);
}
/**
* @param $cp
* @param $slug
* @return Ville|null
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneByMultipleFields($cp, $slug): ?Ville
{
return $this->createQueryBuilder('v')
->andWhere('v.codePostal = :cp')
->setParameter('cp', $cp)
->andWhere('v.slug = :slug OR v.slugComplet = :slug OR v.slugAncienneCommuneLieuDit = :slug')
->setParameter('slug', $slug)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
/**
* @param $cp
* @param $slug
* @return Ville|null
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getVilleByOldSlug($cp, $slug): ?Ville
{
return $this->createQueryBuilder('v')
->andWhere('v.codePostal = :cp')
->setParameter('cp', $cp)
->andWhere('v.slug LIKE :slug OR v.slugAncienneCommuneLieuDit LIKE :slug')
->setParameter('slug', '%' . $slug . '%')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
/**
* @param $cp
* @param $slug
* @return Ville|null
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getVilleBySlug($code, $slug): ?array
{
return $this->createQueryBuilder('v')
->leftJoin('v.departement','dpt')
->andWhere('dpt.code = :code')
->setParameter('code', $code)
->andWhere('v.slug LIKE :slug OR v.slugAncienneCommuneLieuDit LIKE :slug')
->setParameter('slug', '%' . $slug . '%')
->getQuery()
->getResult();
}
/**
* @return Ville|null
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getAllVillesByInsee()
{
return $this->createQueryBuilder('v')
->where('v.insee IS NOT NULL')
->getQuery()
->getResult();
}
/**
* @return Ville|null
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function getVilleByCodeInsee($insee = null)
{
$qb = $this->createQueryBuilder('v');
if ($insee) {
$qb->where('v.codeInsee = :codeInsee')
->setParameter('codeInsee', $insee);
}
return $qb->getQuery()->getOneOrNullResult();
}
}