Comment on page
Create a query by instantiating the
// Setup query
$page = 2;
$query = (new Query())
The above example does the following:
- Queries the URI:
- Sets a few fields to enable automated pagination
- Tells the query that data should be returned from the
// Run directly via a query
$posts = $query->getCollection();
// Or run via a query manager
$posts = $query->getCollection('posts');
- Runs the query
- Returns a collection of results along with a pagination object
In the example above the total results data is set as
[meta][total_results]which is a property path pointing to
There are a number of base query classes you can use to construct queries. It's recommended to create your own child query classes where you can set up the query in your constructor. Base query classes do not set constructors to make creating your own query classes easier.
Intended for REST API queries. Requires a
Strata\Data\Http\Restdata provider. Defaults to a GET query.
Intended for GraphQL queries. Requires a
Intended for GraphQL mutation queries. Requires a
By default, mutation queries are set to not run concurrently and do not cache.
A query has a number of methods to set it up. All setup methods return the
Queryobject so you can use a fluent interface and chain methods together.
Each query must have a data provider and a URI in order to run.
Set the data provider to use with the query. This must be an object of the
Set the URI to use for this query.
string $uriURI to run the query, relative to the data provider base URL
There are a lot of optional settings you can apply to a query.
Add a single parameter to send with this query. This is sent as a GET param with the request.
string $keyParam name
mixed $valueParam value
Set array of parameters to send with this query. These are sent as GET parameters with the request.
Set options for the HTTP request for just this query.
For example to set headers:
'headers' => [
'Content-Type' => 'text/plain'
To set an auth bearer token:
'auth_bearer' => 'ABC123'
All queries default to running concurrently when used with a query manager, you can disable this by calling
bool $concurrent = true
Mark the query as a sub-request, this suppresses errors in the HTTP request.
bool $subRequestDefaults to true
Set fields to return for this query. To use this method you must first set the field parameter.
$query->setFields(['name', 'title', 'email']);
Default functionality is to set the GET param
fields=name,title,emailfor the request.
The parameter to set to define fields to return with
setFields. This defaults to "fields".
Set the root property path to retrieve data from, e.g.
'[data]'to return data from the array key 'data' in the response. If this is not called then data is read from the root element.
string $pathProperty path to root data element
Set pagination current page data.
string|int $currentPageProperty path in returned data to current page, or the actual value
// Set page to the current page variable
// Or set to a data field returned by the query
Set pagination results per page data.
string|int $resultsPerPageProperty path in returned data to results per page, or the actual value
Set pagination total results data.
string|int $totalResultsProperty path in returned data to total results, or the actual value
By default pagination data is read from the returned data. Some API responses set pagination data in the headers rather than the response data. Use this method to tell the query to retrieve pagination data from headers.
bool $paginationDataFromHeadersDefaults to true
When arrays are passed to params they are automatically converted into strings, separate by ','
You can alter the character used to separate multiple values with this method.
By default, automatic caching for data requests happens if the data provider used by the query has caching enabled. This can also be controlled on the query level.
Manually set this query to cache. If no valid cache is set to the data provider this does nothing.
?int $lifetime = nullLifetime in seconds for the cache
Manually set this query to not cache.
Set the following cache tags when storing this query to the cache. These are added to any other cache tags already set.
array $tags = 
Query responses are lazy loaded for performance. If the cache is enabled, query responses are immediately fetched. If a live HTTP request is required then queries are executed when you access data.
You can also manually run a query via the
You can return a single item of data using
get(). This automatically retrieves and decodes the query response and returns data.
This normally returns an array, though the return type is not fixed so child query classes have flexibility on what data is returned.
Collectionobject which contains an iterable collection of data and pagination information.
You can set the root property path to select collection data from in the response. If you don't set this in your query then the root data element is used as the collection (this will throw an exception if it is not iterable).
$collection = $manager->getCollection('queryName');
$pagination = $collection->getPagination();
You can find out if the query response was returned from the cache via
$query->isHit()which returns a boolean. This is a shortcut to the
You can also get access to the response object itself via the
getResponse()method. This returns a
CacheableResponseobject. This is identical to a standard Symfony
HttpClientresponse with the addition of the
isHit()method to indicate whether the response came from the cache or was requested live.
You can use standard
HttpClientmethods to convert data to an array:
$response = $query->getResponse();
$data = $query->getDataProvider()->toArray();
Or you can grab the data provider for this query and use the decode method.
$response = $query->getResponse();
$data = $query->getDataProvider()->decode($response);
By default, once a query is run it stores its response and does not re-run the live request again.
If you want to force the query to re-run, simply call the
Then any subsequent calls on
getResponse()will re-run the live query. Please note if caching is enabled for this query, then a cached response will still be returned if it exists.