Sort results by sort_order with Doctrine phpcr-odm

315 Views Asked by At

Using a Document class with PHPCR-ODM, it is possible to fetch results with the class repository, the results are automatically sorted by a field sort_order that is not in the Document class but in the database schema.

Example of a query logged in the Symfony Profiler:

SELECT path FROM phpcr_nodes WHERE parent = ? AND workspace_name = ? ORDER BY sort_order ASC

I have this simply query built with queryBuilder :

$qb->from()
        ->document('AppBundle\Document\Product', 'product')
        ->end()
        ->where()
        ->neq()->field('product.type')->literal('category');
$query = $qb->getQuery();

The result is not sorted by the field sort_order like other queries, and I can't use the orderBy method as this is not a field of the Document class.

So, how can I sort my results?

1

There are 1 best solutions below

5
On

Who said you can't use orderBy()? You should read the docs because certainly it's possible. Using your same code and assuming the sort column as sort_order see below:

$qb->from()
   ->document('AppBundle\Document\Product', 'product')
   ->end()
   ->where()
   ->neq()->field('product.type')->literal('category')
   ->orderBy()->desc()->field('product.sort_order');