Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Query Builder multiple orderBy

I have the following code

public function queryAssociationsSorted($id){
    $qb = $this->createQueryBuilder('a')
            ->leftJoin('a.category', 'c')
            ->where('a.job = :id')
            ->setParameter('id', $id)
            //->addOrderBy('c.rank', 'DESC')
            //->addOrderBy('a.updated', 'DESC')    
            ->add('orderBy','c.rank DESC THEN a.updated DESC')       
            ;

    return $query = $qb->getQuery();
}

and this or the commented out options both only sort by a.updated. I have looked at other posts on this subject and can't find a solution. Can anyone suggest where I am going wrong?

like image 878
Ben Stinton Avatar asked Jun 02 '26 23:06

Ben Stinton


1 Answers

If I understand correctly, and this is the result yu want:


id - rank - updated


xx - 4 - 2014-01-01

xx - 3 - 2014-01-02

xx - 3 - 2014-01-01


This actually works:

$qb = $this->createQueryBuilder('a')
            ->leftJoin('a.category', 'c')
            ->where('a.job = :id')
            ->setParameter('id', $id)
            ->addOrderBy('c.rank', 'DESC')
            ->addOrderBy('a.updated', 'DESC')    
            ;

return $query = $qb->getQuery();
like image 151
Albert Casadessús Avatar answered Jun 06 '26 03:06

Albert Casadessús



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!