<?php
namespace App\Repository;
use DateTime;
use App\Entity\Regles;
use App\Entity\ReglesVerification;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Regles>
*
* @method Regles|null find($id, $lockMode = null, $lockVersion = null)
* @method Regles|null findOneBy(array $criteria, array $orderBy = null)
* @method Regles[] findAll()
* @method Regles[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ReglesRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Regles::class);
}
public function add(Regles $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Regles $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
/**
* @return Regles[] Returns an array of Regles objects
*/
public function findAllActive(): array
{
return $this->createQueryBuilder('r')
->andWhere('r.disabled = :val')->orWhere('r.disabled IS NULL')
->setParameter('val', false)
->getQuery()
->getResult();
}
/**
* @return Regles[] Returns an array of Regles objects
*/
public function findByDate(DateTime $date): array
{
$today = new DateTime($date->format('Y-m-d H:i:s'));
$yesterday = new DateTime($date->format('Y-m-d H:i:s'));
return $this->createQueryBuilder('r')
->leftJoin(ReglesVerification::class, 'rv', Join::WITH, 'r.id = rv.ruleId')
->andWhere('rv.date < :today')
->andWhere('rv.date >= :yesterday')
->setParameter('today', $today->setTime(12, 0, 0, 0))
->setParameter('yesterday', $yesterday->setTime(12, 0, 0, 0)->modify("-1 day"))
->getQuery()
->getResult();
}
// public function findOneBySomeField($value): ?Regles
// {
// return $this->createQueryBuilder('r')
// ->andWhere('r.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}