src/Repository/UserRepository.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  4. use Symfony\Bridge\Doctrine\RegistryInterface;
  5. use App\Entity\User;
  6. use Doctrine\ORM\QueryBuilder;
  7. class UserRepository extends ServiceEntityRepository {
  8.     public function __construct(RegistryInterface $registry)
  9.     {
  10.         parent::__construct($registryUser::class);
  11.     }
  12.     // DATATABLE
  13.     public function datatableCount($search$filters){
  14.         $qb $this->createQueryBuilder("u")
  15.             ->select("COUNT(u)")
  16.             ->leftJoin("u.address""a")
  17.             ->where("u.deletedAt IS NULL");
  18.         $this->applySearch($qb$search);
  19.         $this->applyFilters($qb$filters);
  20.         return $qb->getQuery()->getSingleScalarResult();
  21.     }
  22.     public function datatableList($firstResult$maxResult$orderBy$orderWay$search$filters){
  23.         $qb $this->createQueryBuilder("u")
  24.             ->select("u")
  25.             ->leftJoin("u.address""a")
  26.             ->where("u.deletedAt IS NULL")
  27.             ->setFirstResult($firstResult)
  28.             ->setMaxResults($maxResult);
  29.         $this->applySearch($qb$search);
  30.         $this->applyFilters($qb$filters);
  31.         $this->applyOrder($qb$orderBy$orderWay);
  32.         return $qb->getQuery()->getResult();
  33.     }
  34.     private function applySearch(QueryBuilder $qb$search){
  35.         if($search){
  36.             $qb
  37.                 ->andWhere("u.firstname LIKE :search OR u.lastname LIKE :search OR u.email LIKE :search")
  38.                 ->setParameter("search""%".$search."%");
  39.         }
  40.         return $qb;
  41.     }
  42.     private function applyFilters(QueryBuilder $qb$filters){
  43.         if($filters && is_array($filters)){
  44.             //TODO : NO FILTERS YET
  45.         }
  46.         return $qb;
  47.     }
  48.     private function applyOrder(QueryBuilder $qb$orderBy$orderWay){
  49.         if($orderBy && $orderWay){
  50.             $orderBy "u.".$orderBy;
  51.             $orderWay strtoupper($orderWay);
  52.             $qb->orderBy($orderBy$orderWay);
  53.         }
  54.         return $qb;
  55.     }
  56. }