From 2bfd8e604fc071fbf254eda073b5973bc8ac4e04 Mon Sep 17 00:00:00 2001 From: Dimitri Date: Mon, 16 Sep 2024 18:46:06 +0200 Subject: [PATCH] [Action] change default order logic (#10731) --- src/Api/Doctrine/ActionExtension.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Api/Doctrine/ActionExtension.php b/src/Api/Doctrine/ActionExtension.php index ca42f98a4b..9d2258c85e 100644 --- a/src/Api/Doctrine/ActionExtension.php +++ b/src/Api/Doctrine/ActionExtension.php @@ -44,8 +44,29 @@ public function applyToCollection( $latitude = (float) ($filters['latitude'] ?? null); $longitude = (float) ($filters['longitude'] ?? null); + $queryBuilder + ->addSelect("CASE WHEN $alias.date >= NOW() THEN 0 ELSE 1 END AS HIDDEN is_future") + ->addOrderBy('is_future', 'ASC') + ->addOrderBy($alias.'.date', 'DESC') + ; + if ($latitude && $longitude) { - $this->actionRepository->updateNearByQueryBuilder($queryBuilder, $alias, new Coordinates($latitude, $longitude)); + $subQuery = $queryBuilder->getEntityManager()->createQueryBuilder() + ->from(Action::class, 'a2') + ->select('DISTINCT a2.id') + ; + + $this->actionRepository + ->updateNearByQueryBuilder($subQuery, 'a2', new Coordinates($latitude, $longitude)) + ->setMaxResults(300) + ; + + $ids = $subQuery->getQuery()->getResult(); + + $queryBuilder + ->andWhere("$alias.id IN (:ids)") + ->setParameter('ids', $ids) + ; return; }