try to fix some things

This commit is contained in:
2022-02-28 16:09:52 +01:00
parent cdd21c94c6
commit 1e146f542e
5 changed files with 82 additions and 29 deletions

View File

@@ -15,6 +15,8 @@ use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Person\ResidentialAddress;
use DateTimeImmutable;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
/**
@@ -30,18 +32,37 @@ class ResidentialAddressRepository extends ServiceEntityRepository
parent::__construct($registry, ResidentialAddress::class);
}
public function findCurrentResidentialAddressByPerson(Person $person, ?DateTimeImmutable $at = null): ?ResidentialAddress
/**
* @param Person $person
* @param DateTimeImmutable|null $at
* @return array|ResidentialAddress[]|null
*/
public function findCurrentResidentialAddressByPerson(Person $person, ?DateTimeImmutable $at = null): array
{
return $this->buildQueryFindCurrentResidentialAddresses($person, $at)
->select('ra')
->getQuery()
->getResult();
}
public function buildQueryFindCurrentResidentialAddresses(Person $person, ?DateTimeImmutable $at = null): QueryBuilder
{
$addresses = $this->findBy(['person' => $person], ['startDate' => 'DESC']);
$date = null === $at ? new DateTimeImmutable('today') : $at;
$qb = $this->createQueryBuilder('ra');
foreach ($addresses as $a) {
if ($a->getStartDate() < $date && $a->getEndDate() > $date) {
return $a;
}
}
$dateFilter = $qb->expr()->andX(
$qb->expr()->lte('ra.startDate', ':dateIn'),
$qb->expr()->orX(
$qb->expr()->isNull('ra.endDate'),
$qb->expr()->gte('ra.endDate', ':dateIn')
)
);
return null;
$qb
->where($dateFilter)
->setParameter('dateIn', $date, Types::DATE_IMMUTABLE);
return $qb;
}
// /**