diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/ResidentialAddress.php b/src/Bundle/ChillPersonBundle/Entity/Person/ResidentialAddress.php index 33cc41dc1..cb82f766a 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person/ResidentialAddress.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person/ResidentialAddress.php @@ -19,6 +19,7 @@ use Chill\ThirdPartyBundle\Entity\ThirdParty; use DateTimeImmutable; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; +use Symfony\Component\Serializer\Annotation\Context; /** * @ORM\Entity(repositoryClass=ResidentialAddressRepository::class) @@ -48,6 +49,7 @@ class ResidentialAddress * @ORM\ManyToOne(targetEntity=Person::class) * @ORM\JoinColumn(nullable=true) * @Groups({"read"}) + * @Context(normalizationContext={"groups"={"minimal"}}) */ private ?Person $hostPerson = null; diff --git a/src/Bundle/ChillPersonBundle/Repository/ResidentialAddressRepository.php b/src/Bundle/ChillPersonBundle/Repository/ResidentialAddressRepository.php index 11ea823fd..743a7d9f4 100644 --- a/src/Bundle/ChillPersonBundle/Repository/ResidentialAddressRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/ResidentialAddressRepository.php @@ -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; } // /** diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/Entity/PersonRenderBox.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/Entity/PersonRenderBox.vue index 2f3c191b3..2ded0840b 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/Entity/PersonRenderBox.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/_components/Entity/PersonRenderBox.vue @@ -128,42 +128,42 @@ -