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