Doctrine2: custom orderBy

I would like to get some events ordered and paginated by these criteria:

  • open events less than 20 km away.
  • incoming events less than 20 km away.
  • open events over 20 km
  • incoming events over 20 km

I can work with an open / incoming order, but I cannot cut <20km in the first place, this is how my query builder looks like:

public function fetchByLocation($day, $time, $latitude, $longitude, $page = 1, $itemPerPage = 10)
{
    if(!$page) {
        $page = 1;
    }

    $qb = $this->createQueryBuilder('event')
        ->select('event', 'GEO_DISTANCE(:latitude, :longitude, event.latitude, event.longitude) AS distance')
        ->setParameter('latitude', $latitude)
        ->setParameter('longitude', $longitude)
        ->where('(event.day = :day AND event.start >= :time) OR event.day > :day')
        ->setParameter('day', $day)
        ->setParameter('time', $time)
        ->addOrderBy('event.day', 'asc')
        ->addOrderBy('event.start', 'asc');


    $qb->setFirstResult(($page - 1) * $itemPerPage)
        ->setMaxResults($itemPerPage);

    return $qb->getQuery()->getResult();
}

      

This would provide events opened first, any idea for handling <20 km criteria in the query builder? Should I use my own query? Can I mix both?

Thanks a lot for reviewing my request :)

+3


source to share


1 answer


what you want is to distinguish between events that are "near" and "distant". you have to add



->addSelect('(CASE WHEN distance > 20 then 1 ELSE 0 END) AS is_far')
->addOrderBy('is_far ASC')

      

+4


source







All Articles