Sorting & Pagination

ObjectQuel provides a sort by clause for multi-field ordering and a window operator for page-based pagination. Sorting must be established before pagination produces reliable results.

explanation

Sorting Results

Control result ordering with the sort by clause. Multiple fields are evaluated left-to-right as tie-breakers:

// Single field
$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve (p) where p.active = true
    sort by p.name asc
");

// Multiple fields
$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve (p) where p.active = true
    sort by p.featured desc, p.price asc, p.name asc
");

// Sort by a related entity's property
$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    range of c is App\\Entity\\CategoryEntity via p.category
    retrieve (p) where p.active = true
    sort by c.name asc, p.name asc
");

Pagination

The window operator takes a zero-indexed page number and a page size: window [page], [size]. ObjectQuel calculates the offset as page × size, so page 0 returns the first batch, page 1 the next, and so on:

// First page, 10 items per page
$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve (p) where p.active = true
    sort by p.name asc
    window 0, 10
");

// Third page, 25 items per page (items 51–75)
$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve (p) where p.active = true
    sort by p.name asc
    window 2, 25
");
Always sort before paginating. Without a sort by clause, the database may return rows in an unpredictable order, causing items to shift between pages or appear more than once.