URL Query Syntax
Query Parameters
fields
The fields
parameter allows you to include just the fields you need.
https://acme.com/items?fields=title,author
title,author
offset
The offset
parameter allows you to exclude a given number of the first objects returned from a query. this is commonly used for paging, along with limit
.
https://acme.com/items?offset=10
10
limit
The limit
parameter allows you to limit the amount of objects that are returned from a query. This is commonly used for paging, along with offset
.
https://acme.com/items?limit=100
100
having
For example author
is EQUAL
Jake
AND
likes
is EQUAL
10
:
https://acme.com/items?having={"author":"Jake","likes":10}
{
"author": "Jake",
"likes": 10
}
where
The where
parameter lets you use conditionals. For example:
author
is LIKE
Jake
:
https://acme.com/items?where={"author":{"l":"Jake"}}
{
"author": {
"l": "Jake"
}
}
author
is EQUAL
Jake
https://acme.com/items?where={"author":{"e":"Jake"}}
{
"author": {
"e": "Jake"
}
}
author
is NOT EQUAL
Jake
https://acme.com/items?where={"author":{"ne":"Jake"}}
{
"author": {
"ne": "Jake"
}
}
likes
is GREATER THAN
10
https://acme.com/items?where={"likes":{"gt":10}}
{
"likes": {
"gt": 10
}
}
likes
is LESS THAN
10
https://acme.com/items?where={"likes":{"lt":10}}
{
"likes": {
"lt": 10
}
}
likes
is GREATER THAN OR EQUAL TO
10
https://acme.com/items?where={"likes":{"gte":10}}
{
"likes": {
"gte": 10
}
}
likes
is LESS THAN OR EQUAL TO
10
https://acme.com/items?where={"likes":{"lte":10}}
{
"likes": {
"lte": 10
}
}
or
The or
parameter allows you to specify multiple queries for an object to match in an array.
https://acme.com/items?or=[{"author":{"e":"Jake"}},{author:{"e":"Alex"}}]
[
{
"author": {
"e": "Jake"
}
}, {
author: {
"e": "Alex"
}
}
]
in
The in
parameter allows you to specify an array of possible matches.
https://acme.com/items?in={"author":["Jake","Billy"]}
{
"author": [
"Jake",
"Billy"
]
}
sort
The sort
parameter allows you to order your results by the value of a property. The value can be 1 for ascending sort (lowest first; A-Z, 0-10) or -1 for descending (highest first; Z-A, 10-0).
Descending:
https://acme.com/items?sort={"likes":-1}
{
"likes": -1
}
Ascending:
https://acme.com/items?sort={"likes":1}
{
"likes": 1
}
Using parsed Query
Query is a global object that contains a formatted query based on the urls query params.
Use the phqlQueryParser
to automatically apply all the url query commands to a new Phalcon\Mvc\Model\Query\Builder
instance.
/** @var \Prest\Data\Query $query */
$query = $this->get(Services::QUERY);
/** @var \Prest\QueryParsers\PhqlQueryParser $phqlQueryParser */
$phqlQueryParser = $this->get(Services::PHQL_QUERY_PARSER);
/** @var \Phalcon\Mvc\Model\Query\Builder $phqlBuilder */
$phqlBuilder = $phqlQueryParser->fromQuery($query);
// Resultset
$results = $phqlBuilder->getQuery()->execute();