Varien_Db_Select Class Reference

List of all members.

Public Member Functions

 __construct (Zend_Db_Adapter_Abstract $adapter)
 where ($cond, $value=null, $type=null)
 resetJoinLeft ()
 crossUpdateFromSelect ($table)
 insertFromSelect ($tableName, $fields=array(), $onDuplicate=true)
 deleteFromSelect ($table)
 setPart ($part, $value)

Protected Member Functions

 _resetJoinLeft ()
 _findTableInCond ($table, $cond)


Detailed Description

Definition at line 2 of file Select.php.


Constructor & Destructor Documentation

__construct ( Zend_Db_Adapter_Abstract $  adapter  ) 

Class constructor

Parameters:
Zend_Db_Adapter_Abstract $adapter

Definition at line 9 of file Select.php.

00010     {
00011         parent::__construct($adapter);
00012     }


Member Function Documentation

_findTableInCond ( table,
cond 
) [protected]

Find table name in condition (where, column)

Parameters:
string $table
string $cond
Returns:
bool

Definition at line 152 of file Select.php.

00153     {
00154         $quote = $this->_adapter->getQuoteIdentifierSymbol();
00155 
00156         if (strpos($cond, $quote . $table . $quote . '.') !== false) {
00157             return true;
00158         }
00159 
00160         $position = 0;
00161         $result   = 0;
00162         $needle   = array();
00163         while (is_integer($result)) {
00164             $result = strpos($cond, $table . '.', $position);
00165 
00166             if (is_integer($result)) {
00167                 $needle[] = $result;
00168                 $position = ($result + strlen($table) + 1);
00169             }
00170         }
00171 
00172         if (!$needle) {
00173             return false;
00174         }
00175 
00176         foreach ($needle as $position) {
00177             if ($position == 0) {
00178                 return true;
00179             }
00180             if (!preg_match('#[a-z0-9_]#is', substr($cond, $position - 1, 1))) {
00181                 return true;
00182             }
00183         }
00184 
00185         return false;
00186     }

_resetJoinLeft (  )  [protected]

Definition at line 120 of file Select.php.

00121     {
00122         foreach ($this->_parts[self::FROM] as $tableId => $tableProp) {
00123             if ($tableProp['joinType'] == self::LEFT_JOIN) {
00124                 if ($tableProp['useInCond']) {
00125                     continue;
00126                 }
00127 
00128                 $used = false;
00129                 foreach ($tableProp['joinInTables'] as $table) {
00130                     if (isset($this->_parts[self::FROM][$table])) {
00131                         $used = true;
00132                     }
00133                 }
00134 
00135                 if (!$used) {
00136                     unset($this->_parts[self::FROM][$tableId]);
00137                     return $this->_resetJoinLeft();
00138                 }
00139             }
00140         }
00141 
00142         return $this;
00143     }

crossUpdateFromSelect ( table  ) 

Cross Table Update From Current select

Parameters:
string|array $table
Returns:
string

Definition at line 194 of file Select.php.

00194                                                   {
00195         if (!is_array($table)) {
00196             $table = array($table => $table);
00197         }
00198         $keys = array_keys($table);
00199         $tableAlias = $keys[0];
00200         $tableName  = $table[$keys[0]];
00201 
00202         $sql = "UPDATE `{$tableName}`";
00203         if ($tableAlias != $tableName) {
00204             $sql .= " AS `{$tableAlias}`";
00205         }
00206 
00207         // render FROM
00208         $from = array();
00209 
00210         foreach ($this->_parts[self::FROM] as $correlationName => $table) {
00211             $tmp = '';
00212             $tmp .= ' ' . strtoupper($table['joinType']) . ' ';
00213 
00214             $tmp .= $this->_getQuotedSchema($table['schema']);
00215             $tmp .= $this->_getQuotedTable($table['tableName'], $correlationName);
00216 
00217             // Add join conditions (if applicable)
00218             if (! empty($table['joinCondition'])) {
00219                 $tmp .= ' ' . self::SQL_ON . ' ' . $table['joinCondition'];
00220             }
00221 
00222             // Add the table name and condition add to the list
00223             $from[] = $tmp;
00224         }
00225 
00226         // Add the list of all joins
00227         if (!empty($from)) {
00228             $sql .= implode("\n", $from);
00229         }
00230 
00231         // render UPDATE SET
00232         $columns = array();
00233         foreach ($this->_parts[self::COLUMNS] as $columnEntry) {
00234             list($correlationName, $column, $alias) = $columnEntry;
00235             if (empty($alias)) {
00236                 $alias = $column;
00237             }
00238             if (!$column instanceof Zend_Db_Expr && !empty($correlationName)) {
00239                 $column = $this->_adapter->quoteIdentifier(array($correlationName, $column));
00240             }
00241             $columns[] = $this->_adapter->quoteIdentifier(array($tableAlias, $alias))
00242                 . " = {$column}";
00243         }
00244 
00245         $sql .= "\n SET " . implode(', ', $columns) . "\n";
00246 
00247         // render WHERE
00248         $sql = $this->_renderWhere($sql);
00249 
00250         return $sql;
00251     }

deleteFromSelect ( table  ) 

Retrieve DELETE query from select

Parameters:
string $table The table name or alias
Returns:
string

Definition at line 288 of file Select.php.

00288                                              {
00289         $partsInit = self::$_partsInit;
00290         unset($partsInit[self::DISTINCT]);
00291         unset($partsInit[self::COLUMNS]);
00292 
00293         $sql = 'DELETE ' . $table;
00294         foreach (array_keys($partsInit) as $part) {
00295             $method = '_render' . ucfirst($part);
00296             if (method_exists($this, $method)) {
00297                 $sql = $this->$method($sql);
00298             }
00299         }
00300         return $sql;
00301     }

insertFromSelect ( tableName,
fields = array(),
onDuplicate = true 
)

Insert to table from current select

Parameters:
string $tableName
array $fields
bool $onDuplicate
Returns:
string

Definition at line 261 of file Select.php.

00261                                                                                          {
00262         $sql = "INSERT INTO `{$tableName}` ";
00263         if ($fields) {
00264             $sql .= "(`".join('`,`', $fields) . "`) ";
00265         }
00266 
00267         $sql .= $this->assemble();
00268 
00269         if ($onDuplicate && $fields) {
00270             $sql .= " ON DUPLICATE KEY UPDATE";
00271             $updateFields = array();
00272             foreach ($fields as $field) {
00273                 $field = $this->_adapter->quoteIdentifier($field);
00274                 $updateFields[] = "{$field}=VALUES({$field})";
00275             }
00276             $sql .= " " . join(', ', $updateFields);
00277         }
00278 
00279         return $sql;
00280     }

resetJoinLeft (  ) 

Reset unused LEFT JOIN(s)

Returns:
Varien_Db_Select

Definition at line 63 of file Select.php.

00064     {
00065         foreach ($this->_parts[self::FROM] as $tableId => $tableProp) {
00066             if ($tableProp['joinType'] == self::LEFT_JOIN) {
00067                 $useJoin = false;
00068                 foreach ($this->_parts[self::COLUMNS] as $columnEntry) {
00069                     list($correlationName, $column) = $columnEntry;
00070                     if ($column instanceof Zend_Db_Expr) {
00071                         if ($this->_findTableInCond($tableId, $column)
00072                             || $this->_findTableInCond($tableProp['tableName'], $column)) {
00073                             $useJoin = true;
00074                         }
00075                     }
00076                     else {
00077                         if ($correlationName == $tableId) {
00078                             $useJoin = true;
00079                         }
00080                     }
00081                 }
00082                 foreach ($this->_parts[self::WHERE] as $where) {
00083                     if ($this->_findTableInCond($tableId, $where)
00084                         || $this->_findTableInCond($tableProp['tableName'], $where)) {
00085                         $useJoin = true;
00086                     }
00087                 }
00088 
00089                 $joinUseInCond  = $useJoin;
00090                 $joinInTables   = array();
00091 
00092                 foreach ($this->_parts[self::FROM] as $tableCorrelationName => $table) {
00093                     if ($tableCorrelationName == $tableId) {
00094                         continue;
00095                     }
00096                     if (!empty($table['joinCondition'])) {
00097                         if ($this->_findTableInCond($tableId, $table['joinCondition'])
00098                         || $this->_findTableInCond($tableProp['tableName'], $table['joinCondition'])) {
00099                             $useJoin = true;
00100                             $joinInTables[] = $tableCorrelationName;
00101                         }
00102                     }
00103                 }
00104 
00105                 if (!$useJoin) {
00106                     unset($this->_parts[self::FROM][$tableId]);
00107                 }
00108                 else {
00109                     $this->_parts[self::FROM][$tableId]['useInCond'] = $joinUseInCond;
00110                     $this->_parts[self::FROM][$tableId]['joinInTables'] = $joinInTables;
00111                 }
00112             }
00113         }
00114 
00115         $this->_resetJoinLeft();
00116 
00117         return $this;
00118     }

setPart ( part,
value 
)

Modify (hack) part of the structured information for the currect query

Parameters:
string $part
mixed $value
Returns:
Varien_Db_Select

Definition at line 310 of file Select.php.

00311     {
00312         $part = strtolower($part);
00313         if (!array_key_exists($part, $this->_parts)) {
00314             throw new Zend_Db_Select_Exception("Invalid Select part '$part'");
00315         }
00316         $this->_parts[$part] = $value;
00317         return $this;
00318     }

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 $cond The WHERE condition.
string $value OPTIONAL A single value to quote into the condition.
constant $type OPTIONAL The type of the given value
Returns:
Zend_Db_Select This Zend_Db_Select object.

Definition at line 46 of file Select.php.

00047     {
00048         if (is_null($value) && is_null($type)) {
00049             $value = '';
00050         }
00051         if (is_array($value)) {
00052             $cond = $this->_adapter->quoteInto($cond, $value);
00053             $value = null;
00054         }
00055         return parent::where($cond, $value, $type);
00056     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:25:01 2009 for Magento by  doxygen 1.5.8