72 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\Resource;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* ResourceRepository.
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ResourceRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Resource::class);
}
/**
* @return Resource[]
*/
public function findAllByEntity(Person|Household $entity): array
{
$qb = $this->createQueryBuilder('r');
$property = $entity instanceof Person ? 'person' : 'household';
$qb->where("r.{$property} = :entity")
->setParameter('entity', $entity);
return $qb->getQuery()->getResult();
}
public function findByEntityAndDate(Person|Household $entity, DateTime $date, $sort = null)
{
$qb = $this->createQueryBuilder('c');
$entityStr = $entity instanceof Person ? 'person' : 'household';
$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([
'entity' => $entity,
'date' => $date,
]);
return $qb->getQuery()->getResult();
}
}