<?php

namespace App\Repository;

use App\Entity\CategoriePro;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\Query\Expr;

/**
 * @method CategoriePro|null find($id, $lockMode = null, $lockVersion = null)
 * @method CategoriePro|null findOneBy(array $criteria, array $orderBy = null)
 * @method CategoriePro[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class CategorieProRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, CategoriePro::class);
    }

    public function findAll( array $orderBy = ['libelle' => 'ASC'] )
    {
        return $this->findBy([], $orderBy);
    }

    public function findByLibelle($libelle, $id = null) {
        $qb =  $this->createQueryBuilder('e')
            ->andWhere('unaccent_lower(e.libelle) = unaccent_lower(:libelle)')
            ->setParameter('libelle', $libelle);

        if ($id) {
            $qb->andWhere('e.id <> :id')
                ->setParameter('id', $id);
        }
        return
            $qb->getQuery()->getResult();
    }

    // /**
    //  * @return CategoriePro[] Returns an array of CategoriePro objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('c.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?CategoriePro
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}
