<?php
namespace App\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
use App\Entity\User;
use Doctrine\ORM\QueryBuilder;
class UserRepository extends ServiceEntityRepository {
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, User::class);
}
// DATATABLE
public function datatableCount($search, $filters){
$qb = $this->createQueryBuilder("u")
->select("COUNT(u)")
->leftJoin("u.address", "a")
->where("u.deletedAt IS NULL");
$this->applySearch($qb, $search);
$this->applyFilters($qb, $filters);
return $qb->getQuery()->getSingleScalarResult();
}
public function datatableList($firstResult, $maxResult, $orderBy, $orderWay, $search, $filters){
$qb = $this->createQueryBuilder("u")
->select("u")
->leftJoin("u.address", "a")
->where("u.deletedAt IS NULL")
->setFirstResult($firstResult)
->setMaxResults($maxResult);
$this->applySearch($qb, $search);
$this->applyFilters($qb, $filters);
$this->applyOrder($qb, $orderBy, $orderWay);
return $qb->getQuery()->getResult();
}
private function applySearch(QueryBuilder $qb, $search){
if($search){
$qb
->andWhere("u.firstname LIKE :search OR u.lastname LIKE :search OR u.email LIKE :search")
->setParameter("search", "%".$search."%");
}
return $qb;
}
private function applyFilters(QueryBuilder $qb, $filters){
if($filters && is_array($filters)){
//TODO : NO FILTERS YET
}
return $qb;
}
private function applyOrder(QueryBuilder $qb, $orderBy, $orderWay){
if($orderBy && $orderWay){
$orderBy = "u.".$orderBy;
$orderWay = strtoupper($orderWay);
$qb->orderBy($orderBy, $orderWay);
}
return $qb;
}
}