class DataQuery

An object representing a query of data from the DataObject's supporting database.

Acts as a wrapper over {@link SQLSelect} and performs all of the query generation. Used extensively by {@link DataList}.

Unlike DataList, modifiers on DataQuery modify the object rather than returning a clone. DataList is immutable, DataQuery is mutable.

Methods

__construct(string $dataClass)

Create a new DataQuery.

__clone()

Clone this object

string
dataClass()

Return the {@link DataObject} class that is being queried.

query()

Return the {@link SQLSelect} object that represents the current query; note that it will be a clone of the object.

$this
removeFilterOn(string|array $fieldExpression)

Remove a filter from the query

$this
setQueriedColumns(array $queriedColumns)

No description

getFinalisedQuery(array|null $queriedColumns = null)

Ensure that the query is ready to execute.

execute()

Execute the query and return the result as {@link SS_Query} object.

string
sql(array $parameters = array())

Return this query's SQL

int
count()

Return the number of records in this query.

string
max(string $field)

Return the maximum value of the given field in this DataList

string
min(string $field)

Return the minimum value of the given field in this DataList

string
avg(string $field)

Return the average value of the given field in this DataList

string
sum(string $field)

Return the sum of the values of the given field in this DataList

string
aggregate(string $expression)

Runs a raw aggregate expression. Please handle escaping yourself

firstRow()

Return the first row that would be returned by this full DataQuery Note that this will issue a separate SELECT .

lastRow()

Return the last row that would be returned by this full DataQuery Note that this will issue a separate SELECT .

$this
groupby(string $groupby)

Append a GROUP BY clause to this query.

$this
having(mixed $having)

Append a HAVING clause to this query.

disjunctiveGroup()

Create a disjunctive subgroup.

conjunctiveGroup()

Create a conjunctive subgroup

$this
where(string|array|SQLConditionGroup $filter)

Adds a WHERE clause.

$this
whereAny(string|array|SQLConditionGroup $filter)

Append a WHERE with OR.

$this
sort(string $sort = null, string $direction = null, bool $clear = true)

Set the ORDER BY clause of this query

$this
reverseSort()

Reverse order by clause

$this
limit(int $limit, int $offset)

Set the limit of this query.

$this
distinct(bool $value)

Set whether this query should be distinct or not.

$this
innerJoin(string $table, string $onClause, string $alias = null, int $order = 20, array $parameters = array())

Add an INNER JOIN clause to this query.

$this
leftJoin(string $table, string $onClause, string $alias = null, int $order = 20, array $parameters = array())

Add a LEFT JOIN clause to this query.

static string
applyRelationPrefix(string|array $relation)

Prefix of all joined table aliases. E.g. ->filter('Banner.Image.Title)' Will join the Banner, and then Image relations $relationPrefx will be banner_image_ Each table in the Image chain will be suffixed to this prefix. E.g.

string
applyRelation(string|array $relation, bool $linearOnly = false)

Traverse the relationship fields, and add the table mappings to the query object state. This has to be called in any overloaded {@link SearchFilter->apply()} methods manually.

$this
subtract(DataQuery $subtractQuery, string $field = 'ID')

Removes the result of query from this query.

$this
selectFromTable(string $table, array $fields)

Select the only given fields from the given table.

$this
addSelectFromTable(string $table, array $fields)

Add the given fields from the given table to the select statement.

array
column(string $field = 'ID')

Query the given field column from the database and return as an array.

selectField(string $fieldExpression, string $alias = null)

Select the given field expressions.

$this
setQueryParam(string $key, string|array $value)

Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query.

string
getQueryParam(string $key)

Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query.

array
getQueryParams()

Returns all query parameters

getDataQueryManipulators()

Get query manipulators

$this
pushQueryManipulator(DataQueryManipulator $manipulator)

Assign callback to be invoked in getFinalisedQuery()

Details

at line 73
__construct(string $dataClass)

Create a new DataQuery.

Parameters

string $dataClass The name of the DataObject class that you wish to query

at line 82
__clone()

Clone this object

at line 92
string dataClass()

Return the {@link DataObject} class that is being queried.

Return Value

string

at line 103
SQLSelect query()

Return the {@link SQLSelect} object that represents the current query; note that it will be a clone of the object.

Return Value

SQLSelect

at line 117
$this removeFilterOn(string|array $fieldExpression)

Remove a filter from the query

Parameters

string|array $fieldExpression The predicate of the condition to remove (ignoring parameters). The expression will be considered a match if it's contained within any other predicate.

Return Value

$this

at line 189
$this setQueriedColumns(array $queriedColumns)

Parameters

array $queriedColumns

Return Value

$this

at line 201
SQLSelect getFinalisedQuery(array|null $queriedColumns = null)

Ensure that the query is ready to execute.

Parameters

array|null $queriedColumns Any columns to filter the query by

Return Value

SQLSelect The finalised sql query

at line 425
Query execute()

Execute the query and return the result as {@link SS_Query} object.

Return Value

Query

at line 436
string sql(array $parameters = array())

Return this query's SQL

Parameters

array $parameters Out variable for parameters required for this query

Return Value

string The resulting SQL query (may be paramaterised)

at line 447
int count()

Return the number of records in this query.

Note that this will issue a separate SELECT COUNT() query.

Return Value

int

at line 460
string max(string $field)

Return the maximum value of the given field in this DataList

Parameters

string $field Unquoted database column name. Will be ANSI quoted automatically so must not contain double quotes.

Return Value

string

at line 476
string min(string $field)

Return the minimum value of the given field in this DataList

Parameters

string $field Unquoted database column name. Will be ANSI quoted automatically so must not contain double quotes.

Return Value

string

at line 492
string avg(string $field)

Return the average value of the given field in this DataList

Parameters

string $field Unquoted database column name. Will be ANSI quoted automatically so must not contain double quotes.

Return Value

string

at line 508
string sum(string $field)

Return the sum of the values of the given field in this DataList

Parameters

string $field Unquoted database column name. Will be ANSI quoted automatically so must not contain double quotes.

Return Value

string

at line 524
string aggregate(string $expression)

Runs a raw aggregate expression. Please handle escaping yourself

Parameters

string $expression An aggregate expression, such as 'MAX("Balance")', or a set of them (as an escaped SQL statement)

Return Value

string

at line 535
SQLSelect firstRow()

Return the first row that would be returned by this full DataQuery Note that this will issue a separate SELECT .

.. LIMIT 1 query.

Return Value

SQLSelect

at line 546
SQLSelect lastRow()

Return the last row that would be returned by this full DataQuery Note that this will issue a separate SELECT .

.. LIMIT query.

Return Value

SQLSelect

at line 596
$this groupby(string $groupby)

Append a GROUP BY clause to this query.

Parameters

string $groupby Escaped SQL statement

Return Value

$this

at line 608
$this having(mixed $having)

Append a HAVING clause to this query.

Parameters

mixed $having Predicate(s) to set, as escaped SQL statements or parameterised queries

Return Value

$this

at line 621
DataQuery_SubGroup disjunctiveGroup()

Create a disjunctive subgroup.

That is a subgroup joined by OR

Return Value

DataQuery_SubGroup

at line 633
DataQuery_SubGroup conjunctiveGroup()

Create a conjunctive subgroup

That is a subgroup joined by AND

Return Value

DataQuery_SubGroup

at line 648
$this where(string|array|SQLConditionGroup $filter)

Adds a WHERE clause.

Parameters

string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or paramaterised queries

Return Value

$this

See also

SQLSelect::addWhere() for syntax examples, although DataQuery won't expand multiple arguments as SQLSelect does.

at line 666
$this whereAny(string|array|SQLConditionGroup $filter)

Append a WHERE with OR.

Parameters

string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or paramaterised queries

Return Value

$this

See also

SQLSelect::addWhere() for syntax examples, although DataQuery won't expand multiple method arguments as SQLSelect does.

at line 684
$this sort(string $sort = null, string $direction = null, bool $clear = true)

Set the ORDER BY clause of this query

Parameters

string $sort Column to sort on (escaped SQL statement)
string $direction Direction ("ASC" or "DESC", escaped SQL statement)
bool $clear Clear existing values

Return Value

$this

See also

SQLSelect::orderby()

at line 700
$this reverseSort()

Reverse order by clause

Return Value

$this

at line 713
$this limit(int $limit, int $offset)

Set the limit of this query.

Parameters

int $limit
int $offset

Return Value

$this

at line 725
$this distinct(bool $value)

Set whether this query should be distinct or not.

Parameters

bool $value

Return Value

$this

at line 743
$this innerJoin(string $table, string $onClause, string $alias = null, int $order = 20, array $parameters = array())

Add an INNER JOIN clause to this query.

Parameters

string $table The unquoted table name to join to.
string $onClause The filter for the join (escaped SQL statement)
string $alias An optional alias name (unquoted)
int $order A numerical index to control the order that joins are added to the query; lower order values will cause the query to appear first. The default is 20, and joins created automatically by the ORM have a value of 10.
array $parameters Any additional parameters if the join is a parameterised subquery

Return Value

$this

at line 763
$this leftJoin(string $table, string $onClause, string $alias = null, int $order = 20, array $parameters = array())

Add a LEFT JOIN clause to this query.

Parameters

string $table The unquoted table to join to.
string $onClause The filter for the join (escaped SQL statement).
string $alias An optional alias name (unquoted)
int $order A numerical index to control the order that joins are added to the query; lower order values will cause the query to appear first. The default is 20, and joins created automatically by the ORM have a value of 10.
array $parameters Any additional parameters if the join is a parameterised subquery

Return Value

$this

at line 784
static string applyRelationPrefix(string|array $relation)

Prefix of all joined table aliases. E.g. ->filter('Banner.Image.Title)' Will join the Banner, and then Image relations $relationPrefx will be banner_image_ Each table in the Image chain will be suffixed to this prefix. E.g.

banner_image_File and banner_image_Image

This will be null if no relation is joined. E.g. ->filter('Title')

Parameters

string|array $relation Relation in '.' delimited string, or array of parts

Return Value

string Table prefix

at line 808
string applyRelation(string|array $relation, bool $linearOnly = false)

Traverse the relationship fields, and add the table mappings to the query object state. This has to be called in any overloaded {@link SearchFilter->apply()} methods manually.

Note, that in order to filter against the joined relation user code must use {see tablePrefix()} to get the table alias used for this relation.

Parameters

string|array $relation The array/dot-syntax relation to follow
bool $linearOnly Set to true to restrict to linear relations only. Set this if this relation will be used for sorting, and should not include duplicate rows.

Return Value

string The model class of the related item

at line 1094
$this subtract(DataQuery $subtractQuery, string $field = 'ID')

Removes the result of query from this query.

Parameters

DataQuery $subtractQuery
string $field

Return Value

$this

at line 1114
$this selectFromTable(string $table, array $fields)

Select the only given fields from the given table.

Parameters

string $table Unquoted table name (will be escaped automatically)
array $fields Database column names (will be escaped automatically)

Return Value

$this

at line 1132
$this addSelectFromTable(string $table, array $fields)

Add the given fields from the given table to the select statement.

Parameters

string $table Unquoted table name (will be escaped automatically)
array $fields Database column names (will be escaped automatically)

Return Value

$this

at line 1149
array column(string $field = 'ID')

Query the given field column from the database and return as an array.

Parameters

string $field See {@link expressionForField()}.

Return Value

array List of column values for the specified column

at line 1190
selectField(string $fieldExpression, string $alias = null)

Select the given field expressions.

Parameters

string $fieldExpression String The field to select (escaped SQL statement)
string $alias String The alias of that field (escaped SQL statement)

at line 1210
$this setQueryParam(string $key, string|array $value)

Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query.

It's expected that the $key will be namespaced, e.g, 'Versioned.stage' instead of just 'stage'.

Parameters

string $key
string|array $value

Return Value

$this

at line 1222
string getQueryParam(string $key)

Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query.

Parameters

string $key

Return Value

string

at line 1234
array getQueryParams()

Returns all query parameters

Return Value

array query parameters array

at line 1244
DataQueryManipulator[] getDataQueryManipulators()

Get query manipulators

Return Value

DataQueryManipulator[]

at line 1255
$this pushQueryManipulator(DataQueryManipulator $manipulator)

Assign callback to be invoked in getFinalisedQuery()

Parameters

DataQueryManipulator $manipulator

Return Value

$this