class Query (View source)

Provides convenience for writing custom queries while taking advantage of existing defined schemas.

Methods

__construct(Connection $connection, string $sql)

Query constructor.

withSchema(Schema $schema, string $alias = '')

Returns a new query object with the given schema.

withParameters(array $parameters)

Returns a new query with the given parameters.

withoutSchemas()

Returns a new query object without any schemas.

withoutParameters()

Returns a new query object without any inserted parameters.

fetchResult()

Executes the query and returns a PDOStatement instance for the executed query.

array
fetchRows()

Executes the query and returns all result rows as an associated array.

array
fetchModels(string $alias = '', array $relationships = [])

Executes the query and returns models for each row with optionally included relationships.

array
fetchCallback(callable $callback)

Executes the query and returns an array of results that have been passed through the given callback.

generateRows()

Returns a generator that fetches the rows one by one for memory efficient processing.

generateModels(string $alias = '', array $relationships = [])

Returns a generator that fetches the models one by one for memory efficient processing.

generateCallback(callable $callback)

Returns a generator that returns the result of the callback called for each result row one by one.

Details

__construct(Connection $connection, string $sql)

Query constructor.

Parameters

Connection $connection The database connection
string $sql The SQL query to perform

Query withSchema(Schema $schema, string $alias = '')

Returns a new query object with the given schema.

Parameters

Schema $schema The schema to attach to the new query
string $alias Alias to use for the table in the query

Return Value

Query A new query object with the given schema

Query withParameters(array $parameters)

Returns a new query with the given parameters.

Parameters

array $parameters The list of parameters to add

Return Value

Query A new query object with the given parameters

Query withoutSchemas()

Returns a new query object without any schemas.

Return Value

Query A new query object without any attached schemas

Query withoutParameters()

Returns a new query object without any inserted parameters.

Return Value

Query A new query object without any inserted parameters

PDOStatement fetchResult()

Executes the query and returns a PDOStatement instance for the executed query.

Return Value

PDOStatement Result statement from the executed query

array fetchRows()

Executes the query and returns all result rows as an associated array.

Return Value

array All the result rows as an associated array

array fetchModels(string $alias = '', array $relationships = [])

Executes the query and returns models for each row with optionally included relationships.

Parameters

string $alias The alias of the schema to use for creating models
array $relationships List of unique relationships to fill for the model from the result row

Return Value

array The resulting models from the query

array fetchCallback(callable $callback)

Executes the query and returns an array of results that have been passed through the given callback.

Parameters

callable $callback The callback to call for each result row

Return Value

array The return values from the callback called for each result row

Generator generateRows()

Returns a generator that fetches the rows one by one for memory efficient processing.

Return Value

Generator A generator that fetches the rows one by one

Generator generateModels(string $alias = '', array $relationships = [])

Returns a generator that fetches the models one by one for memory efficient processing.

Parameters

string $alias The alias of the schema to use for creating models
array $relationships List of unique relationships to fill for the model from the result row

Return Value

Generator Generator that fetches the models one by one

Generator generateCallback(callable $callback)

Returns a generator that returns the result of the callback called for each result row one by one.

Parameters

callable $callback The callback to call for each result row

Return Value

Generator Generator that calls the callback for each result row and returns the result