Documentation

Select
in package
implements Stringable

Table of Contents

Interfaces

Stringable

Constants

COLUMNS  : mixed = 'columns'
DISTINCT  : mixed = 'distinct'
FOR_UPDATE  : mixed = 'forupdate'
FROM  : mixed = 'from'
GROUP  : mixed = 'group'
HAVING  : mixed = 'having'
INNER_JOIN  : mixed = 'inner join'
LEFT_JOIN  : mixed = 'left join'
LIMIT_COUNT  : mixed = 'limitcount'
LIMIT_OFFSET  : mixed = 'limitoffset'
ORDER  : mixed = 'order'
RIGHT_JOIN  : mixed = 'right join'
SQL_AND  : mixed = 'AND'
SQL_AS  : mixed = 'AS'
SQL_ASC  : mixed = 'ASC'
SQL_DESC  : mixed = 'DESC'
SQL_DISTINCT  : mixed = 'DISTINCT'
SQL_FOR_UPDATE  : mixed = 'FOR UPDATE'
SQL_FROM  : mixed = 'FROM'
SQL_GROUP_BY  : mixed = 'GROUP BY'
SQL_HAVING  : mixed = 'HAVING'
SQL_ON  : mixed = 'ON'
SQL_OR  : mixed = 'OR'
SQL_ORDER_BY  : mixed = 'ORDER BY'
SQL_SELECT  : mixed = 'SELECT'
SQL_STRAIGHT_JOIN  : mixed = 'STRAIGHT_JOIN'
SQL_UNION_ALL  : mixed = 'ALL'
SQL_WHERE  : mixed = 'WHERE'
SQL_WILDCARD  : mixed = '*'
STRAIGHT_JOIN  : mixed = 'straightjoin'
TYPE_CONDITION  : mixed = 'TYPE_CONDITION'
UNION  : mixed = 'union'
WHERE  : mixed = 'where'

Properties

$_adapter  : AdapterInterface
$_forceExplicitAliases  : bool
Flag to force explicit column aliases for UNION compatibility SQLite requires ORDER BY columns to exactly match result set column names
$_parts  : array<string|int, mixed>
The component parts of a SELECT statement Maintained for backward compatibility with getPart()/setPart()
$_tableCols  : array<string|int, mixed>
Tracks which columns are being select from each table and join

Methods

__construct()  : mixed
__toString()  : string
Converts this object to an SQL SELECT string.
assemble()  : string
Converts this object to an SQL SELECT string.
columns()  : $this
Adds columns to the current query.
crossUpdateFromSelect()  : string
Cross Table Update From Current select
deleteFromSelect()  : string
Retrieve DELETE query from select
distinct()  : self
Makes the query SELECT DISTINCT.
exists()  : self
Add EXISTS clause
forUpdate()  : $this
Makes the query SELECT FOR UPDATE.
from()  : self
Adds a FROM table and optional columns to the query.
getAdapter()  : AdapterInterface
getPart()  : mixed
Get part of the structured information for the current query.
group()  : self
Adds grouping to the query.
having()  : self
Adds a HAVING condition to the query by AND.
insertFromSelect()  : string
Insert to table from current select
insertIgnoreFromSelect()  : string
Generate INSERT IGNORE query to the table from current select
join()  : self
Adds a JOIN table and columns to the query.
joinInner()  : self
Add an INNER JOIN table and columns to the query
joinLeft()  : self
Add a LEFT OUTER JOIN table and columns to the query
joinRight()  : self
Add a RIGHT OUTER JOIN table and columns to the query
limit()  : $this
Sets a limit count and offset to the query.
limitPage()  : $this
Sets the limit and count by page number.
order()  : self
Adds a row order to the query.
orderRand()  : self
Adds the random order to query
orHaving()  : self
Adds a HAVING condition to the query by OR.
orWhere()  : self
Adds a WHERE condition to the query by OR
query()  : StatementInterface
Executes the current select object and returns the result statement
reset()  : self
Clears parts of the SELECT object, or all of it.
resetJoinLeft()  : self
Reset unused LEFT JOIN(s)
setPart()  : self
Modify (hack) part of the structured information for the current query
union()  : self
Adds a UNION clause to the query
useStraightJoin()  : $this
Use a STRAIGHT_JOIN for the SQL Select
where()  : self
Adds a WHERE condition to the query by AND
_expandHavingAliases()  : string
Expand column aliases in HAVING clause to their expressions.
_findTableInCond()  : bool
Find table name in condition (where, column)
_join()  : self
Populate the {@link $_parts} 'join' key
_resetJoinLeft()  : $this
Validate LEFT joins, and remove it if not exists
_tableCols()  : void
Add columns to the query
_uniqueCorrelation()  : string
Generate a unique correlation name
_where()  : array<string|int, mixed>
Internal function for creating WHERE conditions

Constants

COLUMNS

public mixed COLUMNS = 'columns'

DISTINCT

public mixed DISTINCT = 'distinct'

FOR_UPDATE

public mixed FOR_UPDATE = 'forupdate'

GROUP

public mixed GROUP = 'group'

HAVING

public mixed HAVING = 'having'

INNER_JOIN

public mixed INNER_JOIN = 'inner join'

LEFT_JOIN

public mixed LEFT_JOIN = 'left join'

LIMIT_COUNT

public mixed LIMIT_COUNT = 'limitcount'

LIMIT_OFFSET

public mixed LIMIT_OFFSET = 'limitoffset'

ORDER

public mixed ORDER = 'order'

RIGHT_JOIN

public mixed RIGHT_JOIN = 'right join'

SQL_AND

public mixed SQL_AND = 'AND'

SQL_AS

public mixed SQL_AS = 'AS'

SQL_ASC

public mixed SQL_ASC = 'ASC'

SQL_DESC

public mixed SQL_DESC = 'DESC'

SQL_DISTINCT

public mixed SQL_DISTINCT = 'DISTINCT'

SQL_FOR_UPDATE

public mixed SQL_FOR_UPDATE = 'FOR UPDATE'

SQL_FROM

public mixed SQL_FROM = 'FROM'

SQL_GROUP_BY

public mixed SQL_GROUP_BY = 'GROUP BY'

SQL_HAVING

public mixed SQL_HAVING = 'HAVING'

SQL_ON

public mixed SQL_ON = 'ON'

SQL_OR

public mixed SQL_OR = 'OR'

SQL_ORDER_BY

public mixed SQL_ORDER_BY = 'ORDER BY'

SQL_SELECT

public mixed SQL_SELECT = 'SELECT'

SQL_STRAIGHT_JOIN

public mixed SQL_STRAIGHT_JOIN = 'STRAIGHT_JOIN'

SQL_UNION_ALL

public mixed SQL_UNION_ALL = 'ALL'

SQL_WHERE

public mixed SQL_WHERE = 'WHERE'

SQL_WILDCARD

public mixed SQL_WILDCARD = '*'

STRAIGHT_JOIN

public mixed STRAIGHT_JOIN = 'straightjoin'

TYPE_CONDITION

public mixed TYPE_CONDITION = 'TYPE_CONDITION'

UNION

public mixed UNION = 'union'

WHERE

public mixed WHERE = 'where'

Properties

$_forceExplicitAliases

Flag to force explicit column aliases for UNION compatibility SQLite requires ORDER BY columns to exactly match result set column names

protected bool $_forceExplicitAliases = false

$_parts

The component parts of a SELECT statement Maintained for backward compatibility with getPart()/setPart()

protected array<string|int, mixed> $_parts = [self::DISTINCT => false, self::COLUMNS => [], self::FROM => [], self::UNION => [], self::WHERE => [], self::GROUP => [], self::HAVING => [], self::ORDER => [], self::LIMIT_COUNT => null, self::LIMIT_OFFSET => null, self::FOR_UPDATE => false, self::STRAIGHT_JOIN => false]

$_tableCols

Tracks which columns are being select from each table and join

protected array<string|int, mixed> $_tableCols = []

Methods

__toString()

Converts this object to an SQL SELECT string.

public __toString() : string
Attributes
#[Override]
Return values
string

assemble()

Converts this object to an SQL SELECT string.

public assemble() : string
Return values
string

columns()

Adds columns to the current query.

public columns([array<string|int, mixed>|string $cols = '*' ][, string $correlationName = null ]) : $this
Parameters
$cols : array<string|int, mixed>|string = '*'

The columns to select.

$correlationName : string = null

Correlation name or table alias

Return values
$this

crossUpdateFromSelect()

Cross Table Update From Current select

public crossUpdateFromSelect(string|array<string|int, mixed> $table) : string
Parameters
$table : string|array<string|int, mixed>
Return values
string

deleteFromSelect()

Retrieve DELETE query from select

public deleteFromSelect(string $table) : string
Parameters
$table : string
Return values
string

distinct()

Makes the query SELECT DISTINCT.

public distinct([bool $flag = true ]) : self
Parameters
$flag : bool = true

Whether or not the SELECT is DISTINCT (default true).

Return values
self

exists()

Add EXISTS clause

public exists(Select $select, string $joinCondition[, bool $isExists = true ]) : self
Parameters
$select : Select
$joinCondition : string
$isExists : bool = true
Return values
self

forUpdate()

Makes the query SELECT FOR UPDATE.

public forUpdate([bool $flag = true ]) : $this
Parameters
$flag : bool = true

Whether or not the SELECT is FOR UPDATE (default true).

Return values
$this

from()

Adds a FROM table and optional columns to the query.

public from(array<string|int, mixed>|string|Expr|Select $name[, array<string|int, mixed>|string|Expr $cols = '*' ][, string|null $schema = null ]) : self
Parameters
$name : array<string|int, mixed>|string|Expr|Select
$cols : array<string|int, mixed>|string|Expr = '*'
$schema : string|null = null
Return values
self

getPart()

Get part of the structured information for the current query.

public getPart(string $part) : mixed
Parameters
$part : string

group()

Adds grouping to the query.

public group(array<string|int, mixed>|string|Expr $spec) : self
Parameters
$spec : array<string|int, mixed>|string|Expr

The column(s) to group by.

Return values
self

having()

Adds a HAVING condition to the query by AND.

public having(string $cond[, mixed $value = null ][, int $type = null ]) : self
Parameters
$cond : string

The HAVING condition.

$value : mixed = null

OPTIONAL A single value to quote into the condition.

$type : int = null

OPTIONAL The type of the given value

Return values
self

insertFromSelect()

Insert to table from current select

public insertFromSelect(string $tableName[, array<string|int, mixed> $fields = [] ][, bool $onDuplicate = true ]) : string
Parameters
$tableName : string
$fields : array<string|int, mixed> = []
$onDuplicate : bool = true
Return values
string

insertIgnoreFromSelect()

Generate INSERT IGNORE query to the table from current select

public insertIgnoreFromSelect(string $tableName[, array<string|int, mixed> $fields = [] ]) : string
Parameters
$tableName : string
$fields : array<string|int, mixed> = []
Return values
string

join()

Adds a JOIN table and columns to the query.

public join(array<string|int, mixed>|string|Expr|Select $name, string|null $cond[, array<string|int, mixed>|string|Expr $cols = self::SQL_WILDCARD ][, string|null $schema = null ]) : self
Parameters
$name : array<string|int, mixed>|string|Expr|Select
$cond : string|null
$cols : array<string|int, mixed>|string|Expr = self::SQL_WILDCARD
$schema : string|null = null
Return values
self

joinInner()

Add an INNER JOIN table and columns to the query

public joinInner(array<string|int, mixed>|string|Expr|Select $name, string|null $cond[, array<string|int, mixed>|string|Expr $cols = self::SQL_WILDCARD ][, string|null $schema = null ]) : self
Parameters
$name : array<string|int, mixed>|string|Expr|Select
$cond : string|null
$cols : array<string|int, mixed>|string|Expr = self::SQL_WILDCARD
$schema : string|null = null
Return values
self

joinLeft()

Add a LEFT OUTER JOIN table and columns to the query

public joinLeft(array<string|int, mixed>|string|Expr|Select $name, string $cond[, array<string|int, mixed>|string|Expr $cols = self::SQL_WILDCARD ][, string|null $schema = null ]) : self
Parameters
$name : array<string|int, mixed>|string|Expr|Select
$cond : string
$cols : array<string|int, mixed>|string|Expr = self::SQL_WILDCARD
$schema : string|null = null
Return values
self

joinRight()

Add a RIGHT OUTER JOIN table and columns to the query

public joinRight(array<string|int, mixed>|string|Expr|Select $name, string $cond[, array<string|int, mixed>|string|Expr $cols = self::SQL_WILDCARD ][, string|null $schema = null ]) : self
Parameters
$name : array<string|int, mixed>|string|Expr|Select
$cond : string
$cols : array<string|int, mixed>|string|Expr = self::SQL_WILDCARD
$schema : string|null = null
Return values
self

limit()

Sets a limit count and offset to the query.

public limit([int $count = null ][, int $offset = null ]) : $this
Parameters
$count : int = null

OPTIONAL The number of rows to return.

$offset : int = null

OPTIONAL Start returning after this many rows.

Return values
$this

limitPage()

Sets the limit and count by page number.

public limitPage(int $page, int $rowCount) : $this
Parameters
$page : int

Limit results to this page number.

$rowCount : int

Use this many rows per page.

Return values
$this

order()

Adds a row order to the query.

public order(array<string|int, mixed>|string|Expr $spec) : self
Parameters
$spec : array<string|int, mixed>|string|Expr

The column(s) and direction to order by.

Return values
self

orderRand()

Adds the random order to query

public orderRand([string|null $field = null ]) : self
Parameters
$field : string|null = null
Return values
self

orHaving()

Adds a HAVING condition to the query by OR.

public orHaving(string $cond[, mixed $value = null ][, int $type = null ]) : self
Parameters
$cond : string

The HAVING condition.

$value : mixed = null

OPTIONAL A single value to quote into the condition.

$type : int = null

OPTIONAL The type of the given value

Return values
self

orWhere()

Adds a WHERE condition to the query by OR

public orWhere(string $cond[, mixed $value = null ][, mixed $type = null ]) : self
Parameters
$cond : string
$value : mixed = null
$type : mixed = null
Return values
self

query()

Executes the current select object and returns the result statement

public query([array<string|int, mixed> $bind = [] ]) : StatementInterface
Parameters
$bind : array<string|int, mixed> = []
Return values
StatementInterface

reset()

Clears parts of the SELECT object, or all of it.

public reset([string|null $part = null ]) : self
Parameters
$part : string|null = null
Return values
self

resetJoinLeft()

Reset unused LEFT JOIN(s)

public resetJoinLeft() : self
Return values
self

setPart()

Modify (hack) part of the structured information for the current query

public setPart(string $part, mixed $value) : self
Parameters
$part : string
$value : mixed
Return values
self

union()

Adds a UNION clause to the query

public union([array<string|int, mixed>|Select $select = [] ][, string|null $type = null ]) : self
Parameters
$select : array<string|int, mixed>|Select = []
$type : string|null = null
Return values
self

useStraightJoin()

Use a STRAIGHT_JOIN for the SQL Select

public useStraightJoin([bool $flag = true ]) : $this
Parameters
$flag : bool = true

Whether or not the SELECT use STRAIGHT_JOIN (default true).

Return values
$this

where()

Adds a WHERE condition to the query by AND

public where(string $cond[, mixed $value = null ][, mixed $type = null ]) : self
Parameters
$cond : string
$value : mixed = null
$type : mixed = null
Return values
self

_expandHavingAliases()

Expand column aliases in HAVING clause to their expressions.

protected _expandHavingAliases(string $having) : string

PostgreSQL doesn't allow column aliases in HAVING clauses (e.g., HAVING cnt > 1 where cnt is an alias for COUNT(*)). This method replaces aliases with their actual expressions.

Parameters
$having : string
Return values
string

_findTableInCond()

Find table name in condition (where, column)

protected _findTableInCond(string $table, string|Expr $cond) : bool
Parameters
$table : string
$cond : string|Expr
Return values
bool

_join()

Populate the {@link $_parts} 'join' key

protected _join(string $type, array<string|int, mixed>|string|Expr|Select $name, string|null $cond, array<string|int, mixed>|string|Expr $cols[, string|null $schema = null ]) : self
Parameters
$type : string
$name : array<string|int, mixed>|string|Expr|Select
$cond : string|null
$cols : array<string|int, mixed>|string|Expr
$schema : string|null = null
Return values
self

_resetJoinLeft()

Validate LEFT joins, and remove it if not exists

protected _resetJoinLeft() : $this
Return values
$this

_tableCols()

Add columns to the query

protected _tableCols(string $correlationName, array<string|int, mixed>|string $cols[, string $afterCorrelationName = null ]) : void
Parameters
$correlationName : string

Correlation name or table name

$cols : array<string|int, mixed>|string

The columns to add

$afterCorrelationName : string = null

OPTIONAL place the columns after this correlation

_uniqueCorrelation()

Generate a unique correlation name

protected _uniqueCorrelation(string|array<string|int, mixed>|Expr|Select $name) : string
Parameters
$name : string|array<string|int, mixed>|Expr|Select

A qualified identifier.

Return values
string —

A unique correlation name.

_where()

Internal function for creating WHERE conditions

protected _where(string $cond[, mixed $value = null ][, mixed $type = null ][, bool $bool = true ]) : array<string|int, mixed>
Parameters
$cond : string
$value : mixed = null
$type : mixed = null
$bool : bool = true
Return values
array<string|int, mixed>
On this page

Search results