Range queries

Since limiting queries to a certain range depends on the underlying database engine, we also have the queryRange() method on our database connection service, which we can use to write queries that include ranges:

 $result = $database->queryRange("SELECT * FROM {players}", 0, 10);

In this example, we query for all the players and limit the result set to the first ten records (from 0 to 10). So, with this method, the placeholder value array is the fourth parameter after $from and $count.

Alternatively, using the SELECT query builder, we have a method on the SelectInterface whereby we can specify a range. So, in that format, the preceding query would look like this:

$result = $database->select('players', 'p')  ->fields('p') ->range(0, ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.