<?php | |||||
namespace Lc\SovBundle\Repository; | |||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | |||||
use Doctrine\ORM\QueryBuilder; | |||||
use Knp\Component\Pager\PaginatorInterface; | |||||
abstract class AbstractRepositoryQuery | |||||
{ | |||||
protected ServiceEntityRepository $repository; | |||||
protected QueryBuilder $query; | |||||
protected PaginatorInterface $paginator; | |||||
protected string $id; | |||||
public function __construct(ServiceEntityRepository $repository, string $id, PaginatorInterface $paginator = null) | |||||
{ | |||||
$this->repository = $repository; | |||||
$this->query = $repository->createQueryBuilder($id); | |||||
$this->paginator = $paginator; | |||||
$this->id = $id; | |||||
} | |||||
public function __call(string $name, $params): self | |||||
{ | |||||
foreach ($params as $key => $value) { | |||||
$this->populateDqlId($params[$key]); | |||||
} | |||||
call_user_func_array([$this->query, $name], $params); | |||||
return $this; | |||||
} | |||||
public function create() | |||||
{ | |||||
$class = get_called_class(); | |||||
return new $class($this->repository, $this->paginator); | |||||
} | |||||
public function call(callable $fn): self | |||||
{ | |||||
$fn($this->query, $this); | |||||
return $this; | |||||
} | |||||
public function findOne() | |||||
{ | |||||
return $this->query->getQuery() | |||||
->setMaxResults(1) | |||||
->getOneOrNullResult() | |||||
; | |||||
} | |||||
public function find() | |||||
{ | |||||
return $this->query->getQuery()->getResult(); | |||||
} | |||||
public function paginate(int $page = 1, int $limit = 20) | |||||
{ | |||||
return $this->paginator->paginate($this->query->getQuery(), $page, $limit); | |||||
} | |||||
public function getRepository(): ServiceEntityRepository | |||||
{ | |||||
return $this->repository; | |||||
} | |||||
protected function populateDqlId(&$data) | |||||
{ | |||||
if (is_string($data)) { | |||||
$words = explode(' ', $data); | |||||
foreach ($words as $k => $v) { | |||||
if (isset($v[0]) && '.' === $v[0]) { | |||||
$words[$k] = $this->id.$v; | |||||
} | |||||
} | |||||
$data = implode(' ', $words); | |||||
} elseif (is_array($data)) { | |||||
foreach ($data as $k => $v) { | |||||
$this->populateDqlId($data[$k]); | |||||
} | |||||
} | |||||
return $data; | |||||
} | |||||
} | |||||
namespace Lc\SovBundle\Repository\Reminder; | namespace Lc\SovBundle\Repository\Reminder; | ||||
use Lc\SovBundle\Repository\AbstractRepository; | |||||
use Lc\SovBundle\Model\Reminder\ReminderInterface; | |||||
use App\Entity\Reminder\Reminder; | |||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | |||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface; | |||||
use Doctrine\Persistence\ManagerRegistry; | |||||
/** | |||||
* @method ReminderInterface|null find($id, $lockMode = null, $lockVersion = null) | |||||
* @method ReminderInterface|null findOneBy(array $criteria, array $orderBy = null) | |||||
* @method ReminderInterface[] findAll() | |||||
* @method ReminderInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | |||||
*/ | |||||
class ReminderRepository extends AbstractRepository | |||||
class ReminderRepository extends ServiceEntityRepository implements ServiceEntityRepositoryInterface | |||||
{ | { | ||||
public function getInterfaceClass() | |||||
public function __construct(ManagerRegistry $registry) | |||||
{ | { | ||||
return ReminderInterface::class; | |||||
} | |||||
public function findByUser($user) | |||||
{ | |||||
$qb = $this->createQueryBuilder('e') | |||||
->leftJoin('e.users', 'u') | |||||
->having('COUNT(u.id) = 0') | |||||
->orHaving(':user MEMBER OF e.users') | |||||
->andWhere('e.done = 0') | |||||
->setParameter('user', $user) | |||||
->orderBy('e.dateReminder', 'ASC') | |||||
->groupBy('e.id'); | |||||
return $qb->getQuery()->getResult(); | |||||
} | |||||
public function findByEasyAdminConfigAndUser($crudAction, $crudControllerFqcn, $user, $entityId = null) | |||||
{ | |||||
$qb = $this->createQueryBuilder('e'); | |||||
$qb->leftJoin('e.users', 'u'); | |||||
$qb->having('COUNT(u.id) = 0'); | |||||
$qb->orHaving(':user MEMBER OF e.users'); | |||||
$qb->andWhere('e.done = 0'); | |||||
$qb->andWhere('e.crudAction LIKE :crudAction'); | |||||
$qb->andWhere('e.crudControllerFqcn LIKE :entity'); | |||||
$qb->setParameter('crudAction', $crudAction); | |||||
$qb->setParameter('crudControllerFqcn', $crudControllerFqcn); | |||||
$qb->setParameter('user', $user); | |||||
if ($entityId) { | |||||
$qb->andWhere('e.entityId LIKE :id'); | |||||
$qb->setParameter('entityId', $entityId); | |||||
} | |||||
$qb->orderBy('e.dateReminder', 'ASC'); | |||||
$qb->groupBy('e.id'); | |||||
return $qb->getQuery()->getResult(); | |||||
parent::__construct($registry, Reminder::class); | |||||
} | } | ||||
} | } |
<?php | |||||
namespace Lc\SovBundle\Repository\Reminder; | |||||
use Lc\SovBundle\Repository\AbstractRepository; | |||||
use Lc\SovBundle\Model\Reminder\ReminderInterface; | |||||
/** | |||||
* @method ReminderInterface|null find($id, $lockMode = null, $lockVersion = null) | |||||
* @method ReminderInterface|null findOneBy(array $criteria, array $orderBy = null) | |||||
* @method ReminderInterface[] findAll() | |||||
* @method ReminderInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | |||||
*/ | |||||
class ReminderRepositoryBack extends AbstractRepository | |||||
{ | |||||
public function getInterfaceClass() | |||||
{ | |||||
return ReminderInterface::class; | |||||
} | |||||
public function findByUser($user) | |||||
{ | |||||
$qb = $this->createQueryBuilder('e') | |||||
->leftJoin('e.users', 'u') | |||||
->having('COUNT(u.id) = 0') | |||||
->orHaving(':user MEMBER OF e.users') | |||||
->andWhere('e.done = 0') | |||||
->setParameter('user', $user) | |||||
->orderBy('e.dateReminder', 'ASC') | |||||
->groupBy('e.id'); | |||||
return $qb->getQuery()->getResult(); | |||||
} | |||||
public function findByEasyAdminConfigAndUser($crudAction, $crudControllerFqcn, $user, $entityId = null) | |||||
{ | |||||
$qb = $this->createQueryBuilder('e'); | |||||
$qb->leftJoin('e.users', 'u'); | |||||
$qb->having('COUNT(u.id) = 0'); | |||||
$qb->orHaving(':user MEMBER OF e.users'); | |||||
$qb->andWhere('e.done = 0'); | |||||
$qb->andWhere('e.crudAction LIKE :crudAction'); | |||||
$qb->andWhere('e.crudControllerFqcn LIKE :entity'); | |||||
$qb->setParameter('crudAction', $crudAction); | |||||
$qb->setParameter('crudControllerFqcn', $crudControllerFqcn); | |||||
$qb->setParameter('user', $user); | |||||
if ($entityId) { | |||||
$qb->andWhere('e.entityId LIKE :id'); | |||||
$qb->setParameter('entityId', $entityId); | |||||
} | |||||
$qb->orderBy('e.dateReminder', 'ASC'); | |||||
$qb->groupBy('e.id'); | |||||
return $qb->getQuery()->getResult(); | |||||
} | |||||
} |
<?php | |||||
namespace Lc\SovBundle\Repository\Reminder; | |||||
use Knp\Component\Pager\PaginatorInterface; | |||||
use Lc\SovBundle\Repository\AbstractRepositoryQuery; | |||||
class ReminderRepositoryQuery extends AbstractRepositoryQuery | |||||
{ | |||||
public function __construct(ReminderRepository $repository, PaginatorInterface $paginator) | |||||
{ | |||||
parent::__construct($repository, 'r', $paginator); | |||||
} | |||||
} |
<?php | |||||
namespace Lc\SovBundle\Repository\Reminder; | |||||
/** | |||||
* class ReminderStore. | |||||
* | |||||
* @author Simon Vieille <simon@deblan.fr> | |||||
*/ | |||||
class ReminderStore | |||||
{ | |||||
protected ReminderRepositoryQuery $query; | |||||
public function __construct(ReminderRepositoryQuery $query) | |||||
{ | |||||
$this->query = $query; | |||||
} | |||||
public function getFoo($query = null) | |||||
{ | |||||
if (!$query) { | |||||
$query = $this->query->create(); | |||||
} | |||||
return $query | |||||
->useCustomFilters() | |||||
->andWhere('.description = :description') | |||||
->setParameter(':description', 'ahah') | |||||
->findOne(); | |||||
} | |||||
} |