0

Can someone help me make this symfony doctrine query to return first the event happening today and then the rest of events in DESC order?

Here what I have now:

public function getCompanyQuery($company, $adId)
    {
        $qb = $this->getEntityManager()->createQueryBuilder()
            ->select('a')->from('AppBundle:ScheduledEvents', 'a')
            ->where('a.company = :company')->setParameter('company', $company);

        if ($adId) {
            $qb->andWhere('a.adid = :ad')->setParameter('ad', $adId);
        }

        return $qb->addOrderBy('a.serverDatetime', 'ASC')->addOrderBy('a.status', 'ASC')->getQuery();
    }

====

If it helps I played with a native mysql query which seems to do what I want:

SELECT * FROM tablename WHERE startDate=CURDATE()
UNION ALL
(SELECT * FROM tablename WHERE startDate <> CURDATE() ORDER BY startDate DESC) 

====

Can someone help me change my doctrine query to do what that native mysql query does?

Regards

  • Hello Valentin, The best way to achieve that is to make 2 differents query and merge the results. See https://stackoverflow.com/questions/32384046/how-to-make-a-union-with-doctrine – Weenesta - Mathieu Dormeval Jul 30 '18 at 06:19
  • And if that is the order, why not to return it like: SELECT * FROM tablename WHERE startDate>=CURDATE() ORDER BY startDate DESC Or I'm missing something here and you want to also show past events ? Why make two querys with ugly UNION when you can make one ? – Martin Fasani Aug 07 '18 at 08:07

0 Answers0