Repository navigation
Solo Repository 1.3 adds pages: ReadQuery::page() reads one page of entities with the number of matching rows on all pages, in an order that keeps rows with equal values on the same pages.
Features
-
ReadQuery::page()takes aPageRequest— the page number from 1, its size and its order — and returns aPagewith the items, the total and the request:use Solo\Repository\PageRequest; $page = $products->read()->with(['category'])->page( new PageRequest(page: 3, perPage: 20, orderBy: ['created_at' => 'DESC']), ['price >=' => '50.00'], ); $page->items; // list<Product>, read by created_at DESC, id DESC $page->total; // every matching product, counted only when the page cannot tell
The primary key ends the order in the direction of its last field, unless the order names it, so rows that tie on every requested field fall on the same pages on every read. A short page tells the total by itself: only a full page, or an empty one past the end, runs
COUNT(*), over the same criteria, visibility and locale. A mapping without a primary key reads the requested order as given and rejects a request without one, and a locking read rejectspage(). On PostgreSQL, an index on(created_at, id)keeps such pages index-ordered; MySQL and MariaDB already keep the key in every secondary index. -
Page::of()applies the same counting rule to rows of custom SQL, andPageRequest::orderEndingWith()the same ordering rule, so a query of its own can page the waypage()does.Page::map()replaces the items, for example with what a response shows, and keeps the total and the request.
Upgrade
composer update solophp/repository