Query Array
Oncord's API allows you to query and retrieve sets of database rows in bulk. These functions are usually named getAll()
. For
instance:
\Components\Website\Posts::getAll()
retrieves all posts in your database.
These getAll()
style functions accept a parameter that lets you customise the query, similar to how you might write an SQL
query. This parameter is known as the 'Query Array'.
For instance, to retrieve 10 posts, you can call:
\Components\Website\Posts::getAll(['limit' => 10'])
Usage with a data control
Data controls such as <data:repeater>
and <data:table>
automatically use the Query Array to
populate their data in an efficient manner. These controls inject the query array as the final parameter in your datasource function call.
For instance, <data:repeater paging="true">
allows the user to click "Show More" to view sets
of results at a time. The repeater injects a Query Array into the datasource to set a 'start' and 'limit' to populate itself in an
progressive manner to ensure that queries are efficient.
Similarly, <data:table>
with <data:column ordercolumn="post_title">
allows the user to
reorder data in a table by clicking the heading. A Query Array is automatically injected into the datasource function to 'order' the
data by the column.
It is important to note that data controls always inject the Query Array as the last parameter. So when paging is enabled: datasource="\Components\Website\Posts::getAllForCategory(1)"
will actually invoke \Components\Website\Posts::getAllForCategory(1, ['limit' => 25])
.
You can manipulate the Query Array using attributes on data controls using attributes such as datafilter=""
and dataorder=""
.
For further information on usage with data controls, please see the:
- <data:repeater> reference
- <data:table> reference
Reference
When directly calling a getAll()
style function, you can pass in a Query Array that accepts these properties:
=> ['sale_datetime']
Similar to SELECT in SQL.
=> ['sale_datetime']
By default all columns are returned, so this can be used to exclude specific columns.
Array (
String column,
String comparator,
String value
)
String conjunction
('AND' or 'OR')
=> [
['sale_datetime', '>', 2020],
'AND',
['sale_datetime', '<', 2021]
]
Similar to a WHERE clause in SQL.
See Database Filters for more details and examples.
=> 10
=> 25
Array (
String column,
'ASC' or 'DESC'
)
=> [['sale_datetime', 'asc']]
Similar to ORDER BY in SQL.
=> ['contact_id']
=> 'Fred'
Advanced users only.
Array (
String table,
String column
)
=> [
['customers_contacts',
'contact_id'],
]
Advanced users only.
Database table names are normally the lower-case component name with underscores. Link with primary key as second element in array.
Calling from within PHP
You can make component API calls by specifying your own query array via PHP.
Get all sales, that were not 'adjustment's made after the year 2020.