Db/Table/Select.php
Zend Framework
LICENSE
This source file is subject to the new BSD license that is bundled with this package in the file LICENSE.txt. It is also available through the world-wide-web at this URL: http://framework.zend.com/license/new-bsd If you did not receive a copy of the license and are unable to obtain it through the world-wide-web, please send an email to [email protected] so we can send you a copy immediately.
- Category
- Zend
- Copyright
- Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- License
- New BSD License
- Package
- Zend_Db
- Subpackage
- Select
- Version
- $Id: Select.php 24593 2012-01-05 20:35:02Z matthew $
\Zend_Db_Table_Select
Class for SQL SELECT query manipulation for the Zend_Db_Table component.
- Parent(s)
- \Zend_Db_Select
- Category
- Zend
- Copyright
- Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- License
- New BSD License
Constants

DISTINCT
= 'distinct'
- Inherited_from
- \Zend_Db_Select::DISTINCT

COLUMNS
= 'columns'
- Inherited_from
- \Zend_Db_Select::COLUMNS

UNION
= 'union'
- Inherited_from
- \Zend_Db_Select::UNION

WHERE
= 'where'
- Inherited_from
- \Zend_Db_Select::WHERE

GROUP
= 'group'
- Inherited_from
- \Zend_Db_Select::GROUP

HAVING
= 'having'
- Inherited_from
- \Zend_Db_Select::HAVING

ORDER
= 'order'
- Inherited_from
- \Zend_Db_Select::ORDER

LIMIT_COUNT
= 'limitcount'
- Inherited_from
- \Zend_Db_Select::LIMIT_COUNT

LIMIT_OFFSET
= 'limitoffset'
- Inherited_from
- \Zend_Db_Select::LIMIT_OFFSET

FOR_UPDATE
= 'forupdate'
- Inherited_from
- \Zend_Db_Select::FOR_UPDATE

INNER_JOIN
= 'inner join'
- Inherited_from
- \Zend_Db_Select::INNER_JOIN

LEFT_JOIN
= 'left join'
- Inherited_from
- \Zend_Db_Select::LEFT_JOIN

RIGHT_JOIN
= 'right join'
- Inherited_from
- \Zend_Db_Select::RIGHT_JOIN

FULL_JOIN
= 'full join'
- Inherited_from
- \Zend_Db_Select::FULL_JOIN

CROSS_JOIN
= 'cross join'
- Inherited_from
- \Zend_Db_Select::CROSS_JOIN

NATURAL_JOIN
= 'natural join'
- Inherited_from
- \Zend_Db_Select::NATURAL_JOIN

SQL_WILDCARD
= '*'
- Inherited_from
- \Zend_Db_Select::SQL_WILDCARD

SQL_SELECT
= 'SELECT'
- Inherited_from
- \Zend_Db_Select::SQL_SELECT

SQL_UNION
= 'UNION'
- Inherited_from
- \Zend_Db_Select::SQL_UNION

SQL_UNION_ALL
= 'UNION ALL'
- Inherited_from
- \Zend_Db_Select::SQL_UNION_ALL

SQL_FROM
= 'FROM'
- Inherited_from
- \Zend_Db_Select::SQL_FROM

SQL_WHERE
= 'WHERE'
- Inherited_from
- \Zend_Db_Select::SQL_WHERE

SQL_DISTINCT
= 'DISTINCT'
- Inherited_from
- \Zend_Db_Select::SQL_DISTINCT

SQL_GROUP_BY
= 'GROUP BY'
- Inherited_from
- \Zend_Db_Select::SQL_GROUP_BY

SQL_ORDER_BY
= 'ORDER BY'
- Inherited_from
- \Zend_Db_Select::SQL_ORDER_BY

SQL_HAVING
= 'HAVING'
- Inherited_from
- \Zend_Db_Select::SQL_HAVING

SQL_FOR_UPDATE
= 'FOR UPDATE'
- Inherited_from
- \Zend_Db_Select::SQL_FOR_UPDATE

SQL_AND
= 'AND'
- Inherited_from
- \Zend_Db_Select::SQL_AND

SQL_AS
= 'AS'
- Inherited_from
- \Zend_Db_Select::SQL_AS

SQL_OR
= 'OR'
- Inherited_from
- \Zend_Db_Select::SQL_OR

SQL_ON
= 'ON'
- Inherited_from
- \Zend_Db_Select::SQL_ON

SQL_ASC
= 'ASC'
- Inherited_from
- \Zend_Db_Select::SQL_ASC

SQL_DESC
= 'DESC'
- Inherited_from
- \Zend_Db_Select::SQL_DESC
Properties


\Zend_Db_Adapter_Abstract $_adapter =
- Type
- \Zend_Db_Adapter_Abstract
- Inherited_from
- \Zend_Db_Select::$$_adapter


array $_bind = array()
array()
Details- Type
- array
- Inherited_from
- \Zend_Db_Select::$$_bind


array $_joinTypes = array(self::INNER_JOIN, self::LEFT_JOIN, self::RIGHT_JOIN, self::FULL_JOIN, self::CROSS_JOIN, self::NATURAL_JOIN)
array(self::INNER_JOIN, self::LEFT_JOIN, self::RIGHT_JOIN, self::FULL_JOIN, self::CROSS_JOIN, self::NATURAL_JOIN)
Details- Type
- array
- Inherited_from
- \Zend_Db_Select::$$_joinTypes


array $_parts = array()
Initialized to the $_partsInit array in the constructor.
array()
Details- Type
- array
- Inherited_from
- \Zend_Db_Select::$$_parts


array $_partsInit = array(self::DISTINCT => false, self::COLUMNS => array(), self::UNION => array(), self::FROM => array(), self::WHERE => array(), self::GROUP => array(), self::HAVING => array(), self::ORDER => array(), self::LIMIT_COUNT => null, self::LIMIT_OFFSET => null, self::FOR_UPDATE => false)
The initial values for the $_parts array.
Inherited from: \Zend_Db_Select::$$_partsInitNOTE: It is important for the 'FOR_UPDATE' part to be last to ensure meximum compatibility with database adapters.
array(self::DISTINCT => false, self::COLUMNS => array(), self::UNION => array(), self::FROM => array(), self::WHERE => array(), self::GROUP => array(), self::HAVING => array(), self::ORDER => array(), self::LIMIT_COUNT => null, self::LIMIT_OFFSET => null, self::FOR_UPDATE => false)
Details- Type
- array
- Inherited_from
- \Zend_Db_Select::$$_partsInit


array $_tableCols = array()
Tracks which columns are being select from each table and join.
Inherited from: \Zend_Db_Select::$$_tableColsarray()
Details- Type
- array
- Inherited_from
- \Zend_Db_Select::$$_tableCols


array $_unionTypes = array(self::SQL_UNION, self::SQL_UNION_ALL)
array(self::SQL_UNION, self::SQL_UNION_ALL)
Details- Type
- array
- Inherited_from
- \Zend_Db_Select::$$_unionTypes
Methods


__call(string $method, array $args) : \Zend_Db_Select
Turn magic function calls into non-magic function calls for joinUsing syntax
Inherited from: \Zend_Db_Select::__call()Name | Type | Description |
---|---|---|
$method | string | |
$args | array | OPTIONAL Zend_Db_Table_Select query modifier |
Type | Description |
---|---|
\Zend_Db_Select |
Exception | Description |
---|---|
\Zend_Db_Select_Exception | If an invalid method is called. |


_getDummyTable() : array
Type | Description |
---|---|
array |


_getQuotedSchema(string $schema = null) : string | null
Name | Type | Description |
---|---|---|
$schema | string | The schema name OPTIONAL |
Type | Description |
---|---|
string | null |


_getQuotedTable(string $tableName, string $correlationName = null) : string
Name | Type | Description |
---|---|---|
$tableName | string | The table name |
$correlationName | string | The correlation name OPTIONAL |
Type | Description |
---|---|
string |


_join(null | string $type, array | string | \Zend_Db_Expr $name, string $cond, array | string $cols, string $schema = null) : \Zend_Db_Select
Does the dirty work of populating the join key.
The $name and $cols parameters follow the same logic as described in the from() method.
Name | Type | Description |
---|---|---|
$type | null | string | Type of join; inner, left, and null are currently supported |
$name | array | string | \Zend_Db_Expr | Table name |
$cond | string | Join on this condition |
$cols | array | string | The columns to select from the joined table |
$schema | string | The database name to specify, if any. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object |
Exception | Description |
---|---|
\Zend_Db_Select_Exception |


_joinUsing( $type, $name, $cond, $cols = '*', $schema = null) : \Zend_Db_Select
.. USING... syntax
This is functionality identical to the existing JOIN methods, however the join condition can be passed as a single column name. This method then completes the ON condition by using the same field for the FROM table and the JOIN table.
$select = $db->select()->from('table1')
->joinUsing('table2', 'column1');
// SELECT * FROM table1 JOIN table2 ON table1.column1 = table2.column2
These joins are called by the developer simply by adding 'Using' to the method name. E.g. * joinUsing * joinInnerUsing * joinFullUsing * joinRightUsing * joinLeftUsing
Name | Type | Description |
---|---|---|
$type | ||
$name | ||
$cond | ||
$cols | ||
$schema |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


_renderColumns(string $sql) : string | null
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string | null |


_renderDistinct(string $sql) : string
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string |


_renderForupdate(string $sql) : string
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string |


_renderFrom(string $sql) : string
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string |


_renderGroup(string $sql) : string
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string |


_renderHaving(string $sql) : string
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string |


_renderLimitoffset(string $sql) : string
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string |


_renderOrder(string $sql) : string
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string |


_renderUnion(string $sql) : string
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string |


_renderWhere(string $sql) : string
Name | Type | Description |
---|---|---|
$sql | string | SQL query |
Type | Description |
---|---|
string |


_tableCols( $correlationName, array | string $cols, bool | string $afterCorrelationName = null) : void
Adds to the internal table-to-column mapping array.
Inherited from: \Zend_Db_Select::_tableCols()Name | Type | Description |
---|---|---|
$correlationName | ||
$cols | array | string | The list of columns; preferably as an array, but possibly as a string containing one column. |
$afterCorrelationName | bool | string | True if it should be prepended, a correlation name if it should be inserted |


_uniqueCorrelation(string | array $name) : string
Name | Type | Description |
---|---|---|
$name | string | array | A qualified identifier. |
Type | Description |
---|---|
string | A unique correlation name. |


_where(string $condition, mixed $value = null, string $type = null, boolean $bool = true) : string
Name | Type | Description |
---|---|---|
$condition | string | |
$value | mixed | optional |
$type | string | optional |
$bool | boolean | true = AND, false = OR |
Type | Description |
---|---|
string | clause |


assemble() : string | null
Performs a validation on the select query before passing back to the parent class.
Ensures that only columns from the primary Zend_Db_Table are returned in the result.
Type | Description |
---|---|
string | null | This object as a SELECT string (or null if a string cannot be produced) |


bind(mixed $bind) : \Zend_Db_Select
Name | Type | Description |
---|---|---|
$bind | mixed |
Type | Description |
---|---|
\Zend_Db_Select |


columns(array | string | \Zend_Db_Expr $cols = '*', string $correlationName = null) : \Zend_Db_Select
The parameter can be a single string or Zend_Db_Expr object, or else an array of strings or Zend_Db_Expr objects.
Name | Type | Description |
---|---|---|
$cols | array | string | \Zend_Db_Expr | The columns to select from this table. |
$correlationName | string | Correlation name of target table. OPTIONAL |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


distinct(bool $flag = true) : \Zend_Db_Select
Name | Type | Description |
---|---|---|
$flag | bool | Whether or not the SELECT is DISTINCT (default true). |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


forUpdate(bool $flag = true) : \Zend_Db_Select
Name | Type | Description |
---|---|---|
$flag | bool | Whether or not the SELECT is FOR UPDATE (default true). |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


from(array | string | \Zend_Db_Expr | \Zend_Db_Table_Abstract $name, array | string | \Zend_Db_Expr $cols = self::SQL_WILDCARD, string $schema = null) : \Zend_Db_Table_Select
Adds a FROM table and optional columns to the query.
The table name can be expressed
Name | Type | Description |
---|---|---|
$name | array | string | \Zend_Db_Expr | \Zend_Db_Table_Abstract | The table name or an associative array relating table name to correlation name. |
$cols | array | string | \Zend_Db_Expr | The columns to select from this table. |
$schema | string | The schema name to specify, if any. |
Type | Description |
---|---|
\Zend_Db_Table_Select | This Zend_Db_Table_Select object. |


getAdapter() : \Zend_Db_Adapter_Abstract
Gets the Zend_Db_Adapter_Abstract for this particular Zend_Db_Select object.
Inherited from: \Zend_Db_Select::getAdapter()Type | Description |
---|---|
\Zend_Db_Adapter_Abstract |


getPart(string $part) : mixed
Get part of the structured information for the currect query.
Inherited from: \Zend_Db_Select::getPart()Name | Type | Description |
---|---|---|
$part | string |
Type | Description |
---|---|
mixed |
Exception | Description |
---|---|
\Zend_Db_Select_Exception |


getTable() : \Zend_Db_Table_Abstract
Return the table that created this select object
Type | Description |
---|---|
\Zend_Db_Table_Abstract |


group(array | string $spec) : \Zend_Db_Select
Name | Type | Description |
---|---|---|
$spec | array | string | The column(s) to group by. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


having(string $cond, mixed $value = null, int $type = null) : \Zend_Db_Select
If a value is passed as the second param, it will be quoted and replaced into the condition wherever a question-mark appears. See where() for an example
Name | Type | Description |
---|---|---|
$cond | string | The HAVING condition. |
$value | mixed | OPTIONAL The value to quote into the condition. |
$type | int | OPTIONAL The type of the given value |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


isReadOnly() : boolean
Tests query to determine if expressions or aliases columns exist.
Type | Description |
---|---|
boolean |


join(array | string | \Zend_Db_Expr $name, string $cond, array | string $cols = self::SQL_WILDCARD, string $schema = null) : \Zend_Db_Select
The $name and $cols parameters follow the same logic as described in the from() method.
Name | Type | Description |
---|---|---|
$name | array | string | \Zend_Db_Expr | The table name. |
$cond | string | Join on this condition. |
$cols | array | string | The columns to select from the joined table. |
$schema | string | The database name to specify, if any. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


joinCross(array | string | \Zend_Db_Expr $name, array | string $cols = self::SQL_WILDCARD, string $schema = null) : \Zend_Db_Select
Add a CROSS JOIN table and colums to the query.
Inherited from: \Zend_Db_Select::joinCross()A cross join is a cartesian product; there is no join condition.
The $name and $cols parameters follow the same logic as described in the from() method.
Name | Type | Description |
---|---|---|
$name | array | string | \Zend_Db_Expr | The table name. |
$cols | array | string | The columns to select from the joined table. |
$schema | string | The database name to specify, if any. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


joinFull(array | string | \Zend_Db_Expr $name, string $cond, array | string $cols = self::SQL_WILDCARD, string $schema = null) : \Zend_Db_Select
Add a FULL OUTER JOIN table and colums to the query.
Inherited from: \Zend_Db_Select::joinFull()A full outer join is like combining a left outer join and a right outer join. All rows from both tables are included, paired with each other on the same row of the result set if they satisfy the join condition, and otherwise paired with NULLs in place of columns from the other table.
The $name and $cols parameters follow the same logic as described in the from() method.
Name | Type | Description |
---|---|---|
$name | array | string | \Zend_Db_Expr | The table name. |
$cond | string | Join on this condition. |
$cols | array | string | The columns to select from the joined table. |
$schema | string | The database name to specify, if any. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


joinInner(array | string | \Zend_Db_Expr $name, string $cond, array | string $cols = self::SQL_WILDCARD, string $schema = null) : \Zend_Db_Select
Add an INNER JOIN table and colums to the query Rows in both tables are matched according to the expression in the $cond argument.
Inherited from: \Zend_Db_Select::joinInner()The result set is comprised of all cases where rows from the left table match rows from the right table.
The $name and $cols parameters follow the same logic as described in the from() method.
Name | Type | Description |
---|---|---|
$name | array | string | \Zend_Db_Expr | The table name. |
$cond | string | Join on this condition. |
$cols | array | string | The columns to select from the joined table. |
$schema | string | The database name to specify, if any. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


joinLeft(array | string | \Zend_Db_Expr $name, string $cond, array | string $cols = self::SQL_WILDCARD, string $schema = null) : \Zend_Db_Select
Add a LEFT OUTER JOIN table and colums to the query All rows from the left operand table are included, matching rows from the right operand table included, and the columns from the right operand table are filled with NULLs if no row exists matching the left table.
Inherited from: \Zend_Db_Select::joinLeft()The $name and $cols parameters follow the same logic as described in the from() method.
Name | Type | Description |
---|---|---|
$name | array | string | \Zend_Db_Expr | The table name. |
$cond | string | Join on this condition. |
$cols | array | string | The columns to select from the joined table. |
$schema | string | The database name to specify, if any. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


joinNatural(array | string | \Zend_Db_Expr $name, array | string $cols = self::SQL_WILDCARD, string $schema = null) : \Zend_Db_Select
Add a NATURAL JOIN table and colums to the query.
Inherited from: \Zend_Db_Select::joinNatural()A natural join assumes an equi-join across any column(s) that appear with the same name in both tables. Only natural inner joins are supported by this API, even though SQL permits natural outer joins as well.
The $name and $cols parameters follow the same logic as described in the from() method.
Name | Type | Description |
---|---|---|
$name | array | string | \Zend_Db_Expr | The table name. |
$cols | array | string | The columns to select from the joined table. |
$schema | string | The database name to specify, if any. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


joinRight(array | string | \Zend_Db_Expr $name, string $cond, array | string $cols = self::SQL_WILDCARD, string $schema = null) : \Zend_Db_Select
Add a RIGHT OUTER JOIN table and colums to the query.
Inherited from: \Zend_Db_Select::joinRight()Right outer join is the complement of left outer join. All rows from the right operand table are included, matching rows from the left operand table included, and the columns from the left operand table are filled with NULLs if no row exists matching the right table.
The $name and $cols parameters follow the same logic as described in the from() method.
Name | Type | Description |
---|---|---|
$name | array | string | \Zend_Db_Expr | The table name. |
$cond | string | Join on this condition. |
$cols | array | string | The columns to select from the joined table. |
$schema | string | The database name to specify, if any. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


limit(int $count = null, int $offset = null) : \Zend_Db_Select
Name | Type | Description |
---|---|---|
$count | int | OPTIONAL The number of rows to return. |
$offset | int | OPTIONAL Start returning after this many rows. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


limitPage(int $page, int $rowCount) : \Zend_Db_Select
Name | Type | Description |
---|---|---|
$page | int | Limit results to this page number. |
$rowCount | int | Use this many rows per page. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


orHaving(string $cond, mixed $value = null, int $type = null) : \Zend_Db_Select
Otherwise identical to orHaving().
Name | Type | Description |
---|---|---|
$cond | string | The HAVING condition. |
$value | mixed | OPTIONAL The value to quote into the condition. |
$type | int | OPTIONAL The type of the given value |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |
- See
- \global\having()


orWhere(string $cond, mixed $value = null, int $type = null) : \Zend_Db_Select
Otherwise identical to where().
Name | Type | Description |
---|---|---|
$cond | string | The WHERE condition. |
$value | mixed | OPTIONAL The value to quote into the condition. |
$type | int | OPTIONAL The type of the given value |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |
- See
- \global\where()


order(mixed $spec) : \Zend_Db_Select
Name | Type | Description |
---|---|---|
$spec | mixed | The column(s) and direction to order by. |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


query(integer $fetchMode = null, mixed $bind = array()) : \PDO_Statement | \Zend_Db_Statement
Executes the current select object and returns the result
Inherited from: \Zend_Db_Select::query()Name | Type | Description |
---|---|---|
$fetchMode | integer | OPTIONAL |
$bind | mixed | An array of data to bind to the placeholders. |
Type | Description |
---|---|
\PDO_Statement | \Zend_Db_Statement |


reset(string $part = null) : \Zend_Db_Select
Clear parts of the Select object, or an individual part.
Inherited from: \Zend_Db_Select::reset()Name | Type | Description |
---|---|---|
$part | string | OPTIONAL |
Type | Description |
---|---|
\Zend_Db_Select |


setIntegrityCheck( $flag = true) : \Zend_Db_Select
Sets the integrity check flag.
Setting this flag to false skips the checks for table joins, allowing 'hybrid' table rows to be created.
Name | Type | Description |
---|---|---|
$flag |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


setTable( $table) : \Zend_Db_Select
Sets the primary table name and retrieves the table schema.
Name | Type | Description |
---|---|---|
$table |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


union(array $select = array(), $type = self::SQL_UNION) : \Zend_Db_Select
The first parameter has to be an array of Zend_Db_Select or sql query strings.
$sql1 = $db->select();
$sql2 = "SELECT ...";
$select = $db->select()
->union(array($sql1, $sql2))
->order("id");
Name | Type | Description |
---|---|---|
$select | array | Array of select clauses for the union. |
$type |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |


where(string $cond, mixed $value = null, int $type = null) : \Zend_Db_Select
If a value is passed as the second param, it will be quoted and replaced into the condition wherever a question-mark appears. Array values are quoted and comma-separated.
// simplest but non-secure
$select->where("id = $id");
// secure (ID is quoted but matched anyway)
$select->where('id = ?', $id);
// alternatively, with named binding
$select->where('id = :id');
Note that it is more correct to use named bindings in your queries for values other than strings. When you use named bindings, don't forget to pass the values when actually making a query:
$db->fetchAll($select, array('id' => 5));
Name | Type | Description |
---|---|---|
$cond | string | The WHERE condition. |
$value | mixed | OPTIONAL The value to quote into the condition. |
$type | int | OPTIONAL The type of the given value |
Type | Description |
---|---|
\Zend_Db_Select | This Zend_Db_Select object. |