Zend Framework  1.12
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Zend_Db_Select Class Reference

Public Member Functions

 __construct (Zend_Db_Adapter_Abstract $adapter)
 Class constructor.
 
 getBind ()
 Get bind variables.
 
 bind ($bind)
 Set bind variables.
 
 distinct ($flag=true)
 Makes the query SELECT DISTINCT.
 
 from ($name, $cols= '*', $schema=null)
 Adds a FROM table and optional columns to the query.
 
 columns ($cols= '*', $correlationName=null)
 Specifies the columns used in the FROM clause.
 
 union ($select=array(), $type=self::SQL_UNION)
 Adds a UNION clause to the query.
 
 join ($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
 Adds a JOIN table and columns to the query.
 
 joinInner ($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
 Add an INNER JOIN table and colums to the query Rows in both tables are matched according to the expression in the $cond argument.
 
 joinLeft ($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
 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.
 
 joinRight ($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
 Add a RIGHT OUTER JOIN table and colums to the query.
 
 joinFull ($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
 Add a FULL OUTER JOIN table and colums to the query.
 
 joinCross ($name, $cols=self::SQL_WILDCARD, $schema=null)
 Add a CROSS JOIN table and colums to the query.
 
 joinNatural ($name, $cols=self::SQL_WILDCARD, $schema=null)
 Add a NATURAL JOIN table and colums to the query.
 
 where ($cond, $value=null, $type=null)
 Adds a WHERE condition to the query by AND.
 
 orWhere ($cond, $value=null, $type=null)
 Adds a WHERE condition to the query by OR.
 
 group ($spec)
 Adds grouping to the query.
 
 having ($cond, $value=null, $type=null)
 Adds a HAVING condition to the query by AND.
 
 orHaving ($cond, $value=null, $type=null)
 Adds a HAVING condition to the query by OR.
 
 order ($spec)
 Adds a row order to the query.
 
 limit ($count=null, $offset=null)
 Sets a limit count and offset to the query.
 
 limitPage ($page, $rowCount)
 Sets the limit and count by page number.
 
 forUpdate ($flag=true)
 Makes the query SELECT FOR UPDATE.
 
 getPart ($part)
 Get part of the structured information for the current query.
 
 query ($fetchMode=null, $bind=array())
 Executes the current select object and returns the result.
 
 assemble ()
 Converts this object to an SQL SELECT string.
 
 reset ($part=null)
 Clear parts of the Select object, or an individual part.
 
 getAdapter ()
 Gets the Zend_Db_Adapter_Abstract for this particular Zend_Db_Select object.
 
 _joinUsing ($type, $name, $cond, $cols= '*', $schema=null)
 Handle JOIN...
 
 __call ($method, array $args)
 Turn magic function calls into non-magic function calls for joinUsing syntax.
 
 __toString ()
 Implements magic method.
 

Public Attributes

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

Protected Member Functions

 _join ($type, $name, $cond, $cols, $schema=null)
 Populate the $_parts 'join' key.
 
 _tableCols ($correlationName, $cols, $afterCorrelationName=null)
 Adds to the internal table-to-column mapping array.
 
 _where ($condition, $value=null, $type=null, $bool=true)
 Internal function for creating the where clause.
 
 _getDummyTable ()
 
 _getQuotedSchema ($schema=null)
 Return a quoted schema name.
 
 _getQuotedTable ($tableName, $correlationName=null)
 Return a quoted table name.
 
 _renderDistinct ($sql)
 Render DISTINCT clause.
 
 _renderColumns ($sql)
 Render DISTINCT clause.
 
 _renderFrom ($sql)
 Render FROM clause.
 
 _renderUnion ($sql)
 Render UNION query.
 
 _renderWhere ($sql)
 Render WHERE clause.
 
 _renderGroup ($sql)
 Render GROUP clause.
 
 _renderHaving ($sql)
 Render HAVING clause.
 
 _renderOrder ($sql)
 Render ORDER clause.
 
 _renderLimitoffset ($sql)
 Render LIMIT OFFSET clause.
 
 _renderForupdate ($sql)
 Render FOR UPDATE clause.
 

Protected Attributes

 $_bind = array()
 
 $_adapter
 
 $_parts = array()
 
 $_tableCols = array()
 

Static Protected Attributes

static $_partsInit
 
static $_joinTypes
 
static $_unionTypes
 

Constructor & Destructor Documentation

__construct ( Zend_Db_Adapter_Abstract  $adapter)

Class constructor.

Parameters
Zend_Db_Adapter_Abstract$adapter

Member Function Documentation

__call (   $method,
array  $args 
)

Turn magic function calls into non-magic function calls for joinUsing syntax.

Parameters
string$method
array$argsOPTIONAL Zend_Db_Table_Select query modifier
Returns
Zend_Db_Select
Exceptions
Zend_Db_Select_ExceptionIf an invalid method is called.

Recognize methods for Has-Many cases: findParent<Class>() findParent<Class>By<Rule>() Use the non-greedy pattern repeat modifier e.g. +?

__toString ( )

Implements magic method.

Returns
string This object as a SELECT string.
_getDummyTable ( )
protected
Returns
array
_getQuotedSchema (   $schema = null)
protected

Return a quoted schema name.

Parameters
string$schemaThe schema name OPTIONAL
Returns
string|null
_getQuotedTable (   $tableName,
  $correlationName = null 
)
protected

Return a quoted table name.

Parameters
string$tableNameThe table name
string$correlationNameThe correlation name OPTIONAL
Returns
string
_join (   $type,
  $name,
  $cond,
  $cols,
  $schema = null 
)
protected

Populate the $_parts 'join' key.

Does the dirty work of populating the join key.

The $name and $cols parameters follow the same logic as described in the from() method.

Parameters
null | string$typeType of join; inner, left, and null are currently supported
array | string | Zend_Db_Expr$nameTable name
string$condJoin on this condition
array | string$colsThe columns to select from the joined table
string$schemaThe database name to specify, if any.
Returns
Zend_Db_Select This Zend_Db_Select object
Exceptions
Zend_Db_Select_Exception
See Also
Zend_Db_Select_Exception
Zend_Db_Select_Exception
_joinUsing (   $type,
  $name,
  $cond,
  $cols = '*',
  $schema = null 
)

Handle JOIN...

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

Returns
Zend_Db_Select This Zend_Db_Select object.
_renderColumns (   $sql)
protected

Render DISTINCT clause.

Parameters
string$sqlSQL query
Returns
string|null
_renderDistinct (   $sql)
protected

Render DISTINCT clause.

Parameters
string$sqlSQL query
Returns
string
_renderForupdate (   $sql)
protected

Render FOR UPDATE clause.

Parameters
string$sqlSQL query
Returns
string
_renderFrom (   $sql)
protected

Render FROM clause.

Parameters
string$sqlSQL query
Returns
string
_renderGroup (   $sql)
protected

Render GROUP clause.

Parameters
string$sqlSQL query
Returns
string
_renderHaving (   $sql)
protected

Render HAVING clause.

Parameters
string$sqlSQL query
Returns
string
_renderLimitoffset (   $sql)
protected

Render LIMIT OFFSET clause.

Parameters
string$sqlSQL query
Returns
string
_renderOrder (   $sql)
protected

Render ORDER clause.

Parameters
string$sqlSQL query
Returns
string
_renderUnion (   $sql)
protected

Render UNION query.

Parameters
string$sqlSQL query
Returns
string
_renderWhere (   $sql)
protected

Render WHERE clause.

Parameters
string$sqlSQL query
Returns
string
_tableCols (   $correlationName,
  $cols,
  $afterCorrelationName = null 
)
protected

Adds to the internal table-to-column mapping array.

Parameters
string$tblThe table/join the columns come from.
array | string$colsThe list of columns; preferably as an array, but possibly as a string containing one column.
bool|stringTrue if it should be prepended, a correlation name if it should be inserted
Returns
void
_where (   $condition,
  $value = null,
  $type = null,
  $bool = true 
)
protected

Internal function for creating the where clause.

Parameters
string$condition
mixed$valueoptional
string$typeoptional
boolean$booltrue = AND, false = OR
Returns
string clause
assemble ( )

Converts this object to an SQL SELECT string.

Returns
string|null This object as a SELECT string. (or null if a string cannot be produced.)
bind (   $bind)

Set bind variables.

Parameters
mixed$bind
Returns
Zend_Db_Select
columns (   $cols = '*',
  $correlationName = null 
)

Specifies the columns used in the FROM clause.

The parameter can be a single string or Zend_Db_Expr object, or else an array of strings or Zend_Db_Expr objects.

Parameters
array | string | Zend_Db_Expr$colsThe columns to select from this table.
string$correlationNameCorrelation name of target table. OPTIONAL
Returns
Zend_Db_Select This Zend_Db_Select object.
See Also
Zend_Db_Select_Exception
distinct (   $flag = true)

Makes the query SELECT DISTINCT.

Parameters
bool$flagWhether or not the SELECT is DISTINCT (default true).
Returns
Zend_Db_Select This Zend_Db_Select object.
forUpdate (   $flag = true)

Makes the query SELECT FOR UPDATE.

Parameters
bool$flagWhether or not the SELECT is FOR UPDATE (default true).
Returns
Zend_Db_Select This Zend_Db_Select object.
from (   $name,
  $cols = '*',
  $schema = null 
)

Adds a FROM table and optional columns to the query.

The first parameter $name can be a simple string, in which case the correlation name is generated automatically. If you want to specify the correlation name, the first parameter must be an associative array in which the key is the correlation name, and the value is the physical table name. For example, array('alias' => 'table'). The correlation name is prepended to all columns fetched for this table.

The second parameter can be a single string or Zend_Db_Expr object, or else an array of strings or Zend_Db_Expr objects.

The first parameter can be null or an empty string, in which case no correlation name is generated or prepended to the columns named in the second parameter.

Parameters
array | string | Zend_Db_Expr$nameThe table name or an associative array relating correlation name to table name.
array | string | Zend_Db_Expr$colsThe columns to select from this table.
string$schemaThe schema name to specify, if any.
Returns
Zend_Db_Select This Zend_Db_Select object.
getAdapter ( )

Gets the Zend_Db_Adapter_Abstract for this particular Zend_Db_Select object.

Returns
Zend_Db_Adapter_Abstract
getBind ( )

Get bind variables.

Returns
array
getPart (   $part)

Get part of the structured information for the current query.

Parameters
string$part
Returns
mixed
Exceptions
Zend_Db_Select_Exception
group (   $spec)

Adds grouping to the query.

Parameters
array | string$specThe column(s) to group by.
Returns
Zend_Db_Select This Zend_Db_Select object.
having (   $cond,
  $value = null,
  $type = null 
)

Adds a HAVING condition to the query by AND.

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

Parameters
string$condThe HAVING condition.
mixed$valueOPTIONAL The value to quote into the condition.
int$typeOPTIONAL The type of the given value
Returns
Zend_Db_Select This Zend_Db_Select object.
join (   $name,
  $cond,
  $cols = self::SQL_WILDCARD,
  $schema = null 
)

Adds a JOIN table and columns to the query.

The $name and $cols parameters follow the same logic as described in the from() method.

Parameters
array | string | Zend_Db_Expr$nameThe table name.
string$condJoin on this condition.
array | string$colsThe columns to select from the joined table.
string$schemaThe database name to specify, if any.
Returns
Zend_Db_Select This Zend_Db_Select object.
joinCross (   $name,
  $cols = self::SQL_WILDCARD,
  $schema = null 
)

Add a CROSS JOIN table and colums to the query.

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.

Parameters
array | string | Zend_Db_Expr$nameThe table name.
array | string$colsThe columns to select from the joined table.
string$schemaThe database name to specify, if any.
Returns
Zend_Db_Select This Zend_Db_Select object.
joinFull (   $name,
  $cond,
  $cols = self::SQL_WILDCARD,
  $schema = null 
)

Add a FULL OUTER JOIN table and colums to the query.

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.

Parameters
array | string | Zend_Db_Expr$nameThe table name.
string$condJoin on this condition.
array | string$colsThe columns to select from the joined table.
string$schemaThe database name to specify, if any.
Returns
Zend_Db_Select This Zend_Db_Select object.
joinInner (   $name,
  $cond,
  $cols = self::SQL_WILDCARD,
  $schema = null 
)

Add an INNER JOIN table and colums to the query Rows in both tables are matched according to the expression in the $cond argument.

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.

Parameters
array | string | Zend_Db_Expr$nameThe table name.
string$condJoin on this condition.
array | string$colsThe columns to select from the joined table.
string$schemaThe database name to specify, if any.
Returns
Zend_Db_Select This Zend_Db_Select object.
joinLeft (   $name,
  $cond,
  $cols = self::SQL_WILDCARD,
  $schema = null 
)

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.

The $name and $cols parameters follow the same logic as described in the from() method.

Parameters
array | string | Zend_Db_Expr$nameThe table name.
string$condJoin on this condition.
array | string$colsThe columns to select from the joined table.
string$schemaThe database name to specify, if any.
Returns
Zend_Db_Select This Zend_Db_Select object.
joinNatural (   $name,
  $cols = self::SQL_WILDCARD,
  $schema = null 
)

Add a NATURAL JOIN table and colums to the query.

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.

Parameters
array | string | Zend_Db_Expr$nameThe table name.
array | string$colsThe columns to select from the joined table.
string$schemaThe database name to specify, if any.
Returns
Zend_Db_Select This Zend_Db_Select object.
joinRight (   $name,
  $cond,
  $cols = self::SQL_WILDCARD,
  $schema = null 
)

Add a RIGHT OUTER JOIN table and colums to the query.

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.

Parameters
array | string | Zend_Db_Expr$nameThe table name.
string$condJoin on this condition.
array | string$colsThe columns to select from the joined table.
string$schemaThe database name to specify, if any.
Returns
Zend_Db_Select This Zend_Db_Select object.
limit (   $count = null,
  $offset = null 
)

Sets a limit count and offset to the query.

Parameters
int$countOPTIONAL The number of rows to return.
int$offsetOPTIONAL Start returning after this many rows.
Returns
Zend_Db_Select This Zend_Db_Select object.
limitPage (   $page,
  $rowCount 
)

Sets the limit and count by page number.

Parameters
int$pageLimit results to this page number.
int$rowCountUse this many rows per page.
Returns
Zend_Db_Select This Zend_Db_Select object.
order (   $spec)

Adds a row order to the query.

Parameters
mixed$specThe column(s) and direction to order by.
Returns
Zend_Db_Select This Zend_Db_Select object.
orHaving (   $cond,
  $value = null,
  $type = null 
)

Adds a HAVING condition to the query by OR.

Otherwise identical to orHaving().

Parameters
string$condThe HAVING condition.
mixed$valueOPTIONAL The value to quote into the condition.
int$typeOPTIONAL The type of the given value
Returns
Zend_Db_Select This Zend_Db_Select object.
See Also
having()
orWhere (   $cond,
  $value = null,
  $type = null 
)

Adds a WHERE condition to the query by OR.

Otherwise identical to where().

Parameters
string$condThe WHERE condition.
mixed$valueOPTIONAL The value to quote into the condition.
int$typeOPTIONAL The type of the given value
Returns
Zend_Db_Select This Zend_Db_Select object.
See Also
where()
query (   $fetchMode = null,
  $bind = array() 
)

Executes the current select object and returns the result.

Parameters
integer$fetchModeOPTIONAL
mixed$bindAn array of data to bind to the placeholders.
Returns
PDO_Statement|Zend_Db_Statement
reset (   $part = null)

Clear parts of the Select object, or an individual part.

Parameters
string$partOPTIONAL
Returns
Zend_Db_Select
union (   $select = array(),
  $type = self::SQL_UNION 
)

Adds a UNION clause to the query.

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");

Parameters
array$selectArray of select clauses for the union.
Returns
Zend_Db_Select This Zend_Db_Select object.
where (   $cond,
  $value = null,
  $type = null 
)

Adds a WHERE condition to the query by AND.

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));

Parameters
string$condThe WHERE condition.
mixed$valueOPTIONAL The value to quote into the condition.
int$typeOPTIONAL The type of the given value
Returns
Zend_Db_Select This Zend_Db_Select object.

Member Data Documentation

$_adapter
protected
$_bind = array()
protected
$_joinTypes
staticprotected
Initial value:
= array(
self::INNER_JOIN,
self::LEFT_JOIN,
self::RIGHT_JOIN,
self::FULL_JOIN,
self::CROSS_JOIN,
self::NATURAL_JOIN,
)
$_parts = array()
protected
$_partsInit
staticprotected
Initial value:
= 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
)
$_tableCols = array()
protected
$_unionTypes
staticprotected
Initial value:
= array(
self::SQL_UNION,
self::SQL_UNION_ALL
)
const COLUMNS = 'columns'
const CROSS_JOIN = 'cross join'
const DISTINCT = 'distinct'
const FOR_UPDATE = 'forupdate'
const FROM = 'from'
const FULL_JOIN = 'full join'
const GROUP = 'group'
const HAVING = 'having'
const INNER_JOIN = 'inner join'
const LEFT_JOIN = 'left join'
const LIMIT_COUNT = 'limitcount'
const LIMIT_OFFSET = 'limitoffset'
const NATURAL_JOIN = 'natural join'
const ORDER = 'order'
const RIGHT_JOIN = 'right join'
const SQL_AND = 'AND'
const SQL_AS = 'AS'
const SQL_ASC = 'ASC'
const SQL_DESC = 'DESC'
const SQL_DISTINCT = 'DISTINCT'
const SQL_FOR_UPDATE = 'FOR UPDATE'
const SQL_FROM = 'FROM'
const SQL_GROUP_BY = 'GROUP BY'
const SQL_HAVING = 'HAVING'
const SQL_ON = 'ON'
const SQL_OR = 'OR'
const SQL_ORDER_BY = 'ORDER BY'
const SQL_SELECT = 'SELECT'
const SQL_UNION = 'UNION'
const SQL_UNION_ALL = 'UNION ALL'
const SQL_WHERE = 'WHERE'
const SQL_WILDCARD = '*'
const UNION = 'union'
const WHERE = 'where'