QuerySet API reference¶
This document describes the details of the QuerySet API.
It builds on the material presented in the model and database query guides, so you’’ll probably want to read and understand those documents before reading this one.
Throughout this reference we’ll use the example Weblog models presented in the database query guide.
- When QuerySets are evaluated
- Methods that return new QuerySets
- Methods that do not return QuerySets
- Field lookups
- Query-related tools
Internally, a QuerySet can be constructed, filtered, and generally passed around without actually hitting the database. No database activity actually occurs until you do something to evaluate the queryset.
You can evaluate a QuerySet in the following ways:
A QuerySet is iterable, and it executes its database query the first time you iterate over it. For example, this will print the headline of all entries in the database:
$entries = \App\Models\Entry::objects()->all(); foreach ($entries as $entry) : echo $entry->headline . "<br>"; endforeach;
- dumping the queryset.
A QuerySet is evaluated when you call var_dump(), print_r() or symfony’s dump() on it. This is for convenience so you can immediately see your results when using the API.
A QuerySet is evaluated when you call count() on it. This, as you might expect, returns the length of the result list.
If you only need to determine the number of records in the set (and don’t need the actual objects), it’s much more efficient to handle a count at the database level using SQL’s SELECT COUNT(*). Powerorm provides a count() method for precisely this reason.
Powerorm provides a range of QuerySet refinement methods that modify either the types of results returned by the QuerySet or the way its SQL query is executed.
Returns a new QuerySet containing objects that match the given lookup parameters. Multiple parameters are joined via AND in the underlying SQL statement.
If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects.
Blog::objects() ->filter( not_(['name' => 1]) ->or_(['name__icontains' => 'president']) ->or_(['name__icontains' => 'chancellor']) ) ->limit(0, 5);
Returns a copy of the current QuerySet (or QuerySet subclass). This can be useful in situations where you might want to pass in either a model manager or a QuerySet and do further filtering on the result. After calling all() on either object, you.ll definitely have a QuerySet to work with.
When a QuerySet is evaluated, it typically caches its results. If the data in the database might have changed since a QuerySet was evaluated, you can get updated results for the same query by calling all() on a previously evaluated QuerySet.
- offset - the position to start fetching the record from, if null start fetching for the first record in the table.
- limit - the number of records to fetch
To limit QuerySet to a certain number of results. This is the equivalent of SQL’s LIMIT and OFFSET clauses.
For example, this returns the first 5 objects (LIMIT 5):
This returns the sixth through tenth objects (OFFSET 5 LIMIT 5):
Limiting a QuerySet returns a new QuerySet.
The following QuerySet methods evaluate the QuerySet and return something other than a QuerySet.
Returns the object matching the given lookup parameters, which should be in the format described in Field lookups.
get() raises MultipleObjectsReturned if more than one object was found.
get() raises a DoesNotExist exception if an object wasn’t found for the given parameters.
Returns an integer representing the number of objects in the database matching the QuerySet. The count() method never raises exceptions.
# Returns the total number of entries in the database.
// Returns the total number of entries in the database. echo \App\Models\Entry::objects()->count(); // Returns the number of entries whose headline starts with 'what' echo \App\Models\Entry::objects()->filter(['headline__startswith' => 'what'])->count();
A count() call performs a SELECT COUNT(*) behind the scenes, so you should always use count() rather than loading all of the record into PHP objects and calling count() on the result (unless you need to load the objects into memory anyway, in which case count() will be faster).
Note that if you want the number of items in a QuerySet and are also retrieving model instances from it (for example, by iterating over it), it’s probably more efficient to use count(queryset) which won’t cause an extra database query like Queryset::count() would.
Returns True if the QuerySet contains any results, and False if not.
This tries to perform the query in the simplest and fastest way possible, but it does execute nearly the same query as a normal QuerySet query.
exists() is useful for searches relating to both object membership in a QuerySet and to the existence of any objects in a QuerySet, particularly in the context of a large QuerySet.
The most efficient method of finding whether a model with a unique field (e.g. primary_key) is a member of a QuerySet is:
if(Entry::objects()->filter(['pk'=>123])->exists()): ... code endif;
For an introduction, see models and database queries documentation.
Powerorms’ built-in lookups are listed below. It is also possible to write custom lookups for model fields.
As a convenience when no lookup type is provided (like in
Entry::objects()->get(['id'=>14])) the lookup type is assumed
to be exact.
Exact match. If the value provided for comparison is null, it will be interpreted as an SQL NULL (see isnull for more details).
SELECT ... WHERE id = 14; SELECT ... WHERE id IS NULL;
Takes either true or false, which correspond to SQL queries of IS NULL and IS NOT NULL, respectively.
SELECT ... WHERE id IS NULL;
Case-insensitive containment test.
SELECT ... WHERE blog_text LIKE '%sequi%';
Note this will match the blog_text ‘Sequi honored today’ and ‘sequi honored today’.
In a given list.
SELECT ... WHERE id IN (2,5,3);
You can also use a queryset to dynamically evaluate the list of values instead of providing a list of literal values:
inner_qs = Blog::objects()->filter(['name__icontains'=>'dolor']); entries = Entry::objects()->filter(['blog__in'=>inner_qs)
This queryset will be evaluated as subselect statement:
SELECT ... WHERE blog.id IN (SELECT id FROM ... WHERE NAME LIKE '%dolor%')
SELECT ... WHERE id > 4;
SELECT ... WHERE headline ILIKE 'Will%';
SELECT ... WHERE headline ILIKE '%will'