mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
Fix: more consistency in Resource and Charge repositories
This commit is contained in:
@@ -11,9 +11,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\BudgetBundle\Repository;
|
||||
|
||||
use Chill\BudgetBundle\Entity\Charge;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* ChargeRepository.
|
||||
@@ -21,24 +24,44 @@ use Doctrine\ORM\EntityRepository;
|
||||
* This class was generated by the Doctrine ORM. Add your own custom
|
||||
* repository methods below.
|
||||
*/
|
||||
class ChargeRepository extends EntityRepository
|
||||
class ChargeRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Charge::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Charge[]
|
||||
*/
|
||||
public function findAllByEntity(Person|Household $entity): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('c');
|
||||
|
||||
$property = $entity instanceof Person ? 'person' : 'household';
|
||||
|
||||
$qb->where("c.{$property} = :entity")
|
||||
->setParameter('entity', $entity);
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function findByEntityAndDate($entity, DateTime $date, $sort = null)
|
||||
{
|
||||
$qb = $this->createQueryBuilder('c');
|
||||
|
||||
$entityStr = $entity instanceof Person ? 'person' : 'household';
|
||||
|
||||
$qb->where("c.{$entityStr} = :{$entityStr}")
|
||||
->andWhere('c.startDate < :date')
|
||||
->andWhere('c.startDate < :date OR c.startDate IS NULL');
|
||||
$qb->where("c.{$entityStr} = :entity")
|
||||
->andWhere('c.startDate <= :date')
|
||||
->andWhere('c.endDate > :date OR c.endDate IS NULL');
|
||||
|
||||
if (null !== $sort) {
|
||||
$qb->orderBy($sort);
|
||||
}
|
||||
|
||||
$qb->setParameters([
|
||||
$entityStr => $entity,
|
||||
'entity' => $entity,
|
||||
'date' => $date,
|
||||
]);
|
||||
|
||||
|
Reference in New Issue
Block a user