Search code examples
mongodbsortingsymfony1doctrine-ormodm

Is it possible to use sort() on multiple fields in Doctrine 2 ODM?


I am doing a query on a result document in my doctrine mongodb *odm*. There are two indexed fields in the document which I would like to use in sort. I have written something like:

$results = $this->createQueryBuilder('Document\Score')
            ->sort('finalScore', 'desc')
            ->sort('date', 'desc')
            ->getQuery()
            ->execute();

Here the second sort() function overrides the first one and the designated result is never found.

Thanks in advance for the nice help.


Solution

  • Try this

    $qb = $this->createQueryBuilder('Document\Score');
    $qb->sort(array(
        'finalScore' => 'desc',
        'date'       => 'desc',
    ));
    $results = $qb->getQuery()->execute();