PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Other methods

This section describes other methods of the Zend_Db_Select class that are not covered above: getPart() and reset().

Retrieving Parts of the Select Object

The getPart() method returns a representation of one part of your SQL query. For example, you can use this method to return the array of expressions for the WHERE clause, or the array of columns (or column expressions) that are in the SELECT list, or the values of the count and offset for the LIMIT clause.

The return value is not a string containing a fragment of SQL syntax. The return value is an internal representation, which is typically an array structure containing values and expressions. Each part of the query has a different structure.

The single argument to the getPart() method is a string that identifies which part of the Select query to return. For example, the string 'from' identifies the part of the Select object that stores information about the tables in the FROM clause, including joined tables.

The Zend_Db_Select class defines constants you can use for parts of the SQL query. You can use these constant definitions, or you can the literal strings.

Table 65. Constants used by getPart() and reset()

Constant String value
Zend_Db_Select::DISTINCT 'distinct'
Zend_Db_Select::FOR_UPDATE 'forupdate'
Zend_Db_Select::COLUMNS 'columns'
Zend_Db_Select::FROM 'from'
Zend_Db_Select::WHERE 'where'
Zend_Db_Select::GROUP 'group'
Zend_Db_Select::HAVING 'having'
Zend_Db_Select::ORDER 'order'
Zend_Db_Select::LIMIT_COUNT 'limitcount'
Zend_Db_Select::LIMIT_OFFSET 'limitoffset'

Example 263. Example of the getPart() method

<?php
$select 
$db->select()
             ->
from('products')
             ->
order('product_id');

// You can use a string literal to specify the part
$orderData $select->getPart'order' );

// You can use a constant to specify the same part
$orderData $select->getPartZend_Db_Select::ORDER );

// The return value may be an array structure, not a string.
// Each part has a different structure.
print_r$orderData );

Resetting Parts of the Select Object

The reset() method enables you to clear one specified part of the SQL query, or else clear all parts of the SQL query if you omit the argument.

The single argument is optional. You can specify the part of the query to clear, using the same strings you used in the argument to the getPart() method. The part of the query you specify is reset to a default state.

If you omit the parameter, reset() changes all parts of the query to their default state. This makes the Zend_Db_Select object equivalent to a new object, as though you had just instantiated it.

Example 264. Example of the reset() method

<?php
// Build this query:
//   SELECT p.*
//   FROM "products" AS p
//   ORDER BY "product_name"

$select $db->select()
             ->
from(array('p' => 'products')
             ->
order('product_name');

// Changed requirement, instead order by a different columns:
//   SELECT p.*
//   FROM "products" AS p
//   ORDER BY "product_id"

// Clear one part so we can redefine it
$select->resetZend_Db_Select::ORDER );

// And specify a different column
$select->order('product_id');

// Clear all parts of the query
$select->reset();

Zend Framework