API Documentation

Db/Select.php

Includes Classes 
category
Zend
copyright
Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
license
http://framework.zend.com/license/new-bsd New BSD License
package
Zend_Db
subpackage
Select
version
$Id: Select.php 22853 2010-08-19 19:01:09Z padraic $
Classes
Zend_Db_Select

Description

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 license@zend.com so we can send you a copy immediately.

Zend_Db_Select

category
Zend
copyright
Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
license
http://framework.zend.com/license/new-bsd New BSD License
package
Zend_Db
subpackage
Select
Constants
DISTINCT
COLUMNS
FROM
UNION
WHERE
GROUP
HAVING
ORDER
LIMIT_COUNT
LIMIT_OFFSET
FOR_UPDATE
INNER_JOIN
LEFT_JOIN
RIGHT_JOIN
FULL_JOIN
CROSS_JOIN
NATURAL_JOIN
SQL_WILDCARD
SQL_SELECT
SQL_UNION
SQL_UNION_ALL
SQL_FROM
SQL_WHERE
SQL_DISTINCT
SQL_GROUP_BY
SQL_ORDER_BY
SQL_HAVING
SQL_FOR_UPDATE
SQL_AND
SQL_AS
SQL_OR
SQL_ON
SQL_ASC
SQL_DESC
Properties
$_bind
$_adapter
$_partsInit
$_joinTypes
$_unionTypes
$_parts
$_tableCols
Methods
__construct
getBind
bind
distinct
from
columns
union
join
joinInner
joinLeft
joinRight
joinFull
joinCross
joinNatural
where
orWhere
group
having
orHaving
order
limit
limitPage
forUpdate
getPart
query
assemble
reset
getAdapter
_join
_joinUsing
_uniqueCorrelation
_tableCols
_where
_getDummyTable
_getQuotedSchema
_getQuotedTable
_renderDistinct
_renderColumns
_renderFrom
_renderUnion
_renderWhere
_renderGroup
_renderHaving
_renderOrder
_renderLimitoffset
_renderForupdate
__call
__toString

Description

Class for SQL SELECT generation and results.

Constants

DISTINCT

 DISTINCT = 'distinct'

Details

value
distinct

COLUMNS

 COLUMNS = 'columns'

Details

value
columns

FROM

 FROM = 'from'

Details

value
from

UNION

 UNION = 'union'

Details

value
union

WHERE

 WHERE = 'where'

Details

value
where

GROUP

 GROUP = 'group'

Details

value
group

HAVING

 HAVING = 'having'

Details

value
having

ORDER

 ORDER = 'order'

Details

value
order

LIMIT_COUNT

 LIMIT_COUNT = 'limitcount'

Details

value
limitcount

LIMIT_OFFSET

 LIMIT_OFFSET = 'limitoffset'

Details

value
limitoffset

FOR_UPDATE

 FOR_UPDATE = 'forupdate'

Details

value
forupdate

INNER_JOIN

 INNER_JOIN = 'inner join'

Details

value
inner join

LEFT_JOIN

 LEFT_JOIN = 'left join'

Details

value
left join

RIGHT_JOIN

 RIGHT_JOIN = 'right join'

Details

value
right join

FULL_JOIN

 FULL_JOIN = 'full join'

Details

value
full join

CROSS_JOIN

 CROSS_JOIN = 'cross join'

Details

value
cross join

NATURAL_JOIN

 NATURAL_JOIN = 'natural join'

Details

value
natural join

SQL_WILDCARD

 SQL_WILDCARD = '*'

Details

value
*

SQL_SELECT

 SQL_SELECT = 'SELECT'

Details

value
SELECT

SQL_UNION

 SQL_UNION = 'UNION'

Details

value
UNION

SQL_UNION_ALL

 SQL_UNION_ALL = 'UNION ALL'

Details

value
UNION ALL

SQL_FROM

 SQL_FROM = 'FROM'

Details

value
FROM

SQL_WHERE

 SQL_WHERE = 'WHERE'

Details

value
WHERE

SQL_DISTINCT

 SQL_DISTINCT = 'DISTINCT'

Details

value
DISTINCT

SQL_GROUP_BY

 SQL_GROUP_BY = 'GROUP BY'

Details

value
GROUP BY

SQL_ORDER_BY

 SQL_ORDER_BY = 'ORDER BY'

Details

value
ORDER BY

SQL_HAVING

 SQL_HAVING = 'HAVING'

Details

value
HAVING

SQL_FOR_UPDATE

 SQL_FOR_UPDATE = 'FOR UPDATE'

Details

value
FOR UPDATE

SQL_AND

 SQL_AND = 'AND'

Details

value
AND

SQL_AS

 SQL_AS = 'AS'

Details

value
AS

SQL_OR

 SQL_OR = 'OR'

Details

value
OR

SQL_ON

 SQL_ON = 'ON'

Details

value
ON

SQL_ASC

 SQL_ASC = 'ASC'

Details

value
ASC

SQL_DESC

 SQL_DESC = 'DESC'

Details

value
DESC

Properties

$_adapter

Zend_Db_Adapter_Abstract $_adapter = ''

Zend_Db_Adapter_Abstract object.

Details

$_adapter
Zend_Db_Adapter_Abstract
visibility
protected
default
final
false
static
false

$_bind

array $_bind = 'array'

Bind variables for query

Details

$_bind
array
visibility
protected
default
array
final
false
static
false

$_joinTypes

array $_joinTypes = 'array'

Specify legal join types.

Details

$_joinTypes
array
visibility
protected
default
array
final
false
static
true

$_parts

array $_parts = 'array'

The component parts of a SELECT statement.

Initialized to the $_partsInit array in the constructor.

Details

$_parts
array
visibility
protected
default
array
final
false
static
false

$_partsInit

array $_partsInit = 'array'

The initial values for the $_parts array.

NOTE: It is important for the 'FOR_UPDATE' part to be last to ensure meximum compatibility with database adapters.

Details

$_partsInit
array
visibility
protected
default
array
final
false
static
true

$_tableCols

array $_tableCols = 'array'

Tracks which columns are being select from each table and join.

Details

$_tableCols
array
visibility
protected
default
array
final
false
static
false

$_unionTypes

array $_unionTypes = 'array'

Specify legal union types.

Details

$_unionTypes
array
visibility
protected
default
array
final
false
static
true

Methods

__call

__call( string $method, array $args ) : Zend_Db_Select

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

Arguments
$method
string
$args
array
OPTIONAL Zend_Db_Table_Select query modifier
Details
visibility
public
final
false
static
false
throws
If an invalid method is called.

__construct

__construct( Zend_Db_Adapter_Abstract $adapter ) :

Class constructor

Arguments
$adapter
Zend_Db_Adapter_Abstract
Details
visibility
public
final
false
static
false

__toString

__toString( ) : string

Implements magic method.

Output
string
This object as a SELECT string.
Details
visibility
public
final
false
static
false

_getDummyTable

_getDummyTable( ) : array

Output
array
Details
visibility
protected
final
false
static
false

_getQuotedSchema

_getQuotedSchema( string $schema = null ) : string|null

Return a quoted schema name

Arguments
$schema
string
The schema name OPTIONAL
Output
string|null
Details
visibility
protected
final
false
static
false

_getQuotedTable

_getQuotedTable( string $tableName, string $correlationName = null ) : string

Return a quoted table name

Arguments
$tableName
string
The table name
$correlationName
string
The correlation name OPTIONAL
Output
string
Details
visibility
protected
final
false
static
false

_join

_join( null|string $type, array|string|Zend_Db_Expr $name, string $cond, array|string $cols, string $schema = null ) : Zend_Db_Select

Populate the {@link $_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.

Arguments
$type
nullstring
Type of join; inner, left, and null are currently supported
$name
arraystringZend_Db_Expr
Table name
$cond
string
Join on this condition
$cols
arraystring
The columns to select from the joined table
$schema
string
The database name to specify, if any.
Output
Zend_Db_Select
This Zend_Db_Select object
Details
visibility
protected
final
false
static
false
throws

_joinUsing

_joinUsing(  $type,  $name,  $cond,  $cols = *,  $schema = null ) : Zend_Db_Select

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

Arguments
$type
$name
$cond
$cols
$schema
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

_renderColumns

_renderColumns( string $sql ) : string|null

Render DISTINCT clause

Arguments
$sql
string
SQL query
Output
string|null
Details
visibility
protected
final
false
static
false

_renderDistinct

_renderDistinct( string $sql ) : string

Render DISTINCT clause

Arguments
$sql
string
SQL query
Output
string
Details
visibility
protected
final
false
static
false

_renderForupdate

_renderForupdate( string $sql ) : string

Render FOR UPDATE clause

Arguments
$sql
string
SQL query
Output
string
Details
visibility
protected
final
false
static
false

_renderFrom

_renderFrom( string $sql ) : string

Render FROM clause

Arguments
$sql
string
SQL query
Output
string
Details
visibility
protected
final
false
static
false

_renderGroup

_renderGroup( string $sql ) : string

Render GROUP clause

Arguments
$sql
string
SQL query
Output
string
Details
visibility
protected
final
false
static
false

_renderHaving

_renderHaving( string $sql ) : string

Render HAVING clause

Arguments
$sql
string
SQL query
Output
string
Details
visibility
protected
final
false
static
false

_renderLimitoffset

_renderLimitoffset( string $sql ) : string

Render LIMIT OFFSET clause

Arguments
$sql
string
SQL query
Output
string
Details
visibility
protected
final
false
static
false

_renderOrder

_renderOrder( string $sql ) : string

Render ORDER clause

Arguments
$sql
string
SQL query
Output
string
Details
visibility
protected
final
false
static
false

_renderUnion

_renderUnion( string $sql ) : string

Render UNION query

Arguments
$sql
string
SQL query
Output
string
Details
visibility
protected
final
false
static
false

_renderWhere

_renderWhere( string $sql ) : string

Render WHERE clause

Arguments
$sql
string
SQL query
Output
string
Details
visibility
protected
final
false
static
false

_tableCols

_tableCols(  $correlationName, array|string $cols, bool|string $afterCorrelationName = null ) : void

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

Arguments
$correlationName
$cols
arraystring
The list of columns; preferably as an array, but possibly as a string containing one column.
$afterCorrelationName
boolstring
True if it should be prepended, a correlation name if it should be inserted
Details
visibility
protected
final
false
static
false

_uniqueCorrelation

_uniqueCorrelation( string|array $name ) : string

Generate a unique correlation name

Arguments
$name
stringarray
A qualified identifier.
Output
string
A unique correlation name.
Details
visibility
private
final
false
static
false

_where

_where( string $condition, mixed $value = null, string $type = null, boolean $bool = true ) : string

Internal function for creating the where clause

Arguments
$condition
string
$value
mixed
optional
$type
string
optional
$bool
boolean
true = AND, false = OR
Output
string
clause
Details
visibility
protected
final
false
static
false

assemble

assemble( ) : string|null

Converts this object to an SQL SELECT string.

Output
string|null
This object as a SELECT string. (or null if a string cannot be produced.)
Details
visibility
public
final
false
static
false

bind

bind( mixed $bind ) : Zend_Db_Select

Set bind variables

Arguments
$bind
mixed
Details
visibility
public
final
false
static
false

columns

columns( array|string|Zend_Db_Expr $cols = *, string $correlationName = null ) : Zend_Db_Select

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.

Arguments
$cols
arraystringZend_Db_Expr
The columns to select from this table.
$correlationName
string
Correlation name of target table. OPTIONAL
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

distinct

distinct( bool $flag = true ) : Zend_Db_Select

Makes the query SELECT DISTINCT.

Arguments
$flag
bool
Whether or not the SELECT is DISTINCT (default true).
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

forUpdate

forUpdate( bool $flag = true ) : Zend_Db_Select

Makes the query SELECT FOR UPDATE.

Arguments
$flag
bool
Whether or not the SELECT is FOR UPDATE (default true).
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

from

from( array|string|Zend_Db_Expr $name, array|string|Zend_Db_Expr $cols = *, string $schema = null ) : Zend_Db_Select

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 physical table name, and the value is the correlation name. For example, array('table' => 'alias'). 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.

Arguments
$name
arraystringZend_Db_Expr
The table name or an associative array relating table name to correlation name.
$cols
arraystringZend_Db_Expr
The columns to select from this table.
$schema
string
The schema name to specify, if any.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

getAdapter

getAdapter( ) : Zend_Db_Adapter_Abstract

Gets the Zend_Db_Adapter_Abstract for this particular Zend_Db_Select object.

Details
visibility
public
final
false
static
false

getBind

getBind( ) : array

Get bind variables

Output
array
Details
visibility
public
final
false
static
false

getPart

getPart( string $part ) : mixed

Get part of the structured information for the currect query.

Arguments
$part
string
Output
mixed
Details
visibility
public
final
false
static
false
throws

group

group( array|string $spec ) : Zend_Db_Select

Adds grouping to the query.

Arguments
$spec
arraystring
The column(s) to group by.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

having

having( string $cond ) : Zend_Db_Select

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 {@link where()} for an example

Arguments
$cond
string
The HAVING condition.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

join

join( array|string|Zend_Db_Expr $name, string $cond, array|string $cols = self, string $schema = null ) : Zend_Db_Select

Adds a JOIN table and columns to the query.

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

Arguments
$name
arraystringZend_Db_Expr
The table name.
$cond
string
Join on this condition.
$cols
arraystring
The columns to select from the joined table.
$schema
string
The database name to specify, if any.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

joinCross

joinCross( array|string|Zend_Db_Expr $name, array|string $cols = self, string $schema = null ) : Zend_Db_Select

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.

Arguments
$name
arraystringZend_Db_Expr
The table name.
$cols
arraystring
The columns to select from the joined table.
$schema
string
The database name to specify, if any.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

joinFull

joinFull( array|string|Zend_Db_Expr $name, string $cond, array|string $cols = self, string $schema = null ) : Zend_Db_Select

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.

Arguments
$name
arraystringZend_Db_Expr
The table name.
$cond
string
Join on this condition.
$cols
arraystring
The columns to select from the joined table.
$schema
string
The database name to specify, if any.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

joinInner

joinInner( array|string|Zend_Db_Expr $name, string $cond, array|string $cols = self, 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. 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.

Arguments
$name
arraystringZend_Db_Expr
The table name.
$cond
string
Join on this condition.
$cols
arraystring
The columns to select from the joined table.
$schema
string
The database name to specify, if any.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

joinLeft

joinLeft( array|string|Zend_Db_Expr $name, string $cond, array|string $cols = self, 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.

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

Arguments
$name
arraystringZend_Db_Expr
The table name.
$cond
string
Join on this condition.
$cols
arraystring
The columns to select from the joined table.
$schema
string
The database name to specify, if any.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

joinNatural

joinNatural( array|string|Zend_Db_Expr $name, array|string $cols = self, string $schema = null ) : Zend_Db_Select

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.

Arguments
$name
arraystringZend_Db_Expr
The table name.
$cols
arraystring
The columns to select from the joined table.
$schema
string
The database name to specify, if any.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

joinRight

joinRight( array|string|Zend_Db_Expr $name, string $cond, array|string $cols = self, string $schema = null ) : Zend_Db_Select

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.

Arguments
$name
arraystringZend_Db_Expr
The table name.
$cond
string
Join on this condition.
$cols
arraystring
The columns to select from the joined table.
$schema
string
The database name to specify, if any.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

limit

limit( int $count = null, int $offset = null ) : Zend_Db_Select

Sets a limit count and offset to the query.

Arguments
$count
int
OPTIONAL The number of rows to return.
$offset
int
OPTIONAL Start returning after this many rows.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

limitPage

limitPage( int $page, int $rowCount ) : Zend_Db_Select

Sets the limit and count by page number.

Arguments
$page
int
Limit results to this page number.
$rowCount
int
Use this many rows per page.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

orHaving

orHaving( string $cond ) : Zend_Db_Select

Adds a HAVING condition to the query by OR.

Otherwise identical to orHaving().

Arguments
$cond
string
The HAVING condition.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false
see
having()

orWhere

orWhere( string $cond, mixed $value = null, constant $type = null ) : Zend_Db_Select

Adds a WHERE condition to the query by OR.

Otherwise identical to where().

Arguments
$cond
string
The WHERE condition.
$value
mixed
OPTIONAL The value to quote into the condition.
$type
constant
OPTIONAL The type of the given value
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false
see
where()

order

order( mixed $spec ) : Zend_Db_Select

Adds a row order to the query.

Arguments
$spec
mixed
The column(s) and direction to order by.
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

query

query( integer $fetchMode = null, mixed $bind = array ) : PDO_Statement|Zend_Db_Statement

Executes the current select object and returns the result

Arguments
$fetchMode
integer
OPTIONAL
$bind
mixed
An array of data to bind to the placeholders.
Output
PDO_Statement|Zend_Db_Statement
Details
visibility
public
final
false
static
false

reset

reset( string $part = null ) : Zend_Db_Select

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

Arguments
$part
string
OPTIONAL
Details
visibility
public
final
false
static
false

union

union( array $select = array,  $type = self ) : Zend_Db_Select

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

Arguments
$select
array
Array of select clauses for the union.
$type
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false

where

where( string $cond, mixed $value = null, constant $type = null ) : Zend_Db_Select

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

Arguments
$cond
string
The WHERE condition.
$value
mixed
OPTIONAL The value to quote into the condition.
$type
constant
OPTIONAL The type of the given value
Output
Zend_Db_Select
This Zend_Db_Select object.
Details
visibility
public
final
false
static
false
Documentation was generated by DocBlox.