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.
Class for SQL SELECT generation and results.
DISTINCT = 'distinct'
COLUMNS = 'columns'
FROM = 'from'
UNION = 'union'
WHERE = 'where'
GROUP = 'group'
HAVING = 'having'
ORDER = 'order'
LIMIT_COUNT = 'limitcount'
LIMIT_OFFSET = 'limitoffset'
FOR_UPDATE = 'forupdate'
INNER_JOIN = 'inner join'
LEFT_JOIN = 'left join'
RIGHT_JOIN = 'right join'
FULL_JOIN = 'full join'
CROSS_JOIN = 'cross join'
NATURAL_JOIN = 'natural join'
SQL_WILDCARD = '*'
SQL_SELECT = 'SELECT'
SQL_UNION = 'UNION'
SQL_UNION_ALL = 'UNION ALL'
SQL_FROM = 'FROM'
SQL_WHERE = 'WHERE'
SQL_DISTINCT = 'DISTINCT'
SQL_GROUP_BY = 'GROUP BY'
SQL_ORDER_BY = 'ORDER BY'
SQL_HAVING = 'HAVING'
SQL_FOR_UPDATE = 'FOR UPDATE'
SQL_AND = 'AND'
SQL_AS = 'AS'
SQL_OR = 'OR'
SQL_ON = 'ON'
SQL_ASC = 'ASC'
SQL_DESC = 'DESC'
Zend_Db_Adapter_Abstract $_adapter = ''
Zend_Db_Adapter_Abstract object.
array $_bind = 'array'
Bind variables for query
array $_joinTypes = 'array'
Specify legal join types.
array $_parts = 'array'
The component parts of a SELECT statement.
Initialized to the $_partsInit array in the constructor.
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.
array $_tableCols = 'array'
Tracks which columns are being select from each table and join.
array $_unionTypes = 'array'
Specify legal union types.
__call(
string $method, array $args
)
:
Zend_Db_Select
Turn magic function calls into non-magic function calls for joinUsing syntax
__construct(
Zend_Db_Adapter_Abstract $adapter
)
:
Class constructor
__toString(
)
:
string
Implements magic method.
_getDummyTable(
)
:
array
_getQuotedSchema(
string $schema
=
null
)
:
string|null
Return a quoted schema name
_getQuotedTable(
string $tableName, string $correlationName
=
null
)
:
string
Return a quoted table name
_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.
_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
_renderColumns(
string $sql
)
:
string|null
Render DISTINCT clause
_renderDistinct(
string $sql
)
:
string
Render DISTINCT clause
_renderForupdate(
string $sql
)
:
string
Render FOR UPDATE clause
_renderFrom(
string $sql
)
:
string
Render FROM clause
_renderGroup(
string $sql
)
:
string
Render GROUP clause
_renderHaving(
string $sql
)
:
string
Render HAVING clause
_renderLimitoffset(
string $sql
)
:
string
Render LIMIT OFFSET clause
_renderOrder(
string $sql
)
:
string
Render ORDER clause
_renderUnion(
string $sql
)
:
string
Render UNION query
_renderWhere(
string $sql
)
:
string
Render WHERE clause
_tableCols(
$correlationName, array|string $cols, bool|string $afterCorrelationName
=
null
)
:
void
Adds to the internal table-to-column mapping array.
_uniqueCorrelation(
string|array $name
)
:
string
Generate a unique correlation name
_where(
string $condition, mixed $value
=
null, string $type
=
null, boolean $bool
=
true
)
:
string
Internal function for creating the where clause
assemble(
)
:
string|null
Converts this object to an SQL SELECT string.
bind(
mixed $bind
)
:
Zend_Db_Select
Set bind variables
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.
distinct(
bool $flag
=
true
)
:
Zend_Db_Select
Makes the query SELECT DISTINCT.
forUpdate(
bool $flag
=
true
)
:
Zend_Db_Select
Makes the query SELECT FOR UPDATE.
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.
getAdapter(
)
:
Zend_Db_Adapter_Abstract
Gets the Zend_Db_Adapter_Abstract for this particular Zend_Db_Select object.
getBind(
)
:
array
Get bind variables
getPart(
string $part
)
:
mixed
Get part of the structured information for the currect query.
group(
array|string $spec
)
:
Zend_Db_Select
Adds grouping to the query.
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
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.
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.
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.
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.
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.
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.
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.
limit(
int $count
=
null, int $offset
=
null
)
:
Zend_Db_Select
Sets a limit count and offset to the query.
limitPage(
int $page, int $rowCount
)
:
Zend_Db_Select
Sets the limit and count by page number.
orHaving(
string $cond
)
:
Zend_Db_Select
Adds a HAVING condition to the query by OR.
Otherwise identical to orHaving().
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().
order(
mixed $spec
)
:
Zend_Db_Select
Adds a row order to the query.
query(
integer $fetchMode
=
null, mixed $bind
=
array
)
:
PDO_Statement|Zend_Db_Statement
Executes the current select object and returns the result
reset(
string $part
=
null
)
:
Zend_Db_Select
Clear parts of the Select object, or an individual part.
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");
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));