web/lib/Zend/Db/Select.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Db
    16  * @package    Zend_Db
    17  * @subpackage Select
    17  * @subpackage Select
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Select.php 23254 2010-10-26 12:49:23Z matthew $
    20  * @version    $Id: Select.php 24833 2012-05-30 13:29:41Z adamlundrigan $
    21  */
    21  */
    22 
    22 
    23 
    23 
    24 /**
    24 /**
    25  * @see Zend_Db_Adapter_Abstract
    25  * @see Zend_Db_Adapter_Abstract
    36  * Class for SQL SELECT generation and results.
    36  * Class for SQL SELECT generation and results.
    37  *
    37  *
    38  * @category   Zend
    38  * @category   Zend
    39  * @package    Zend_Db
    39  * @package    Zend_Db
    40  * @subpackage Select
    40  * @subpackage Select
    41  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    41  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    43  */
    43  */
    44 class Zend_Db_Select
    44 class Zend_Db_Select
    45 {
    45 {
    46 
    46 
   205      * Adds a FROM table and optional columns to the query.
   205      * Adds a FROM table and optional columns to the query.
   206      *
   206      *
   207      * The first parameter $name can be a simple string, in which case the
   207      * The first parameter $name can be a simple string, in which case the
   208      * correlation name is generated automatically.  If you want to specify
   208      * correlation name is generated automatically.  If you want to specify
   209      * the correlation name, the first parameter must be an associative
   209      * the correlation name, the first parameter must be an associative
   210      * array in which the key is the physical table name, and the value is
   210      * array in which the key is the correlation name, and the value is
   211      * the correlation name.  For example, array('table' => 'alias').
   211      * the physical table name.  For example, array('alias' => 'table').
   212      * The correlation name is prepended to all columns fetched for this
   212      * The correlation name is prepended to all columns fetched for this
   213      * table.
   213      * table.
   214      *
   214      *
   215      * The second parameter can be a single string or Zend_Db_Expr object,
   215      * The second parameter can be a single string or Zend_Db_Expr object,
   216      * or else an array of strings or Zend_Db_Expr objects.
   216      * or else an array of strings or Zend_Db_Expr objects.
   217      *
   217      *
   218      * The first parameter can be null or an empty string, in which case
   218      * The first parameter can be null or an empty string, in which case
   219      * no correlation name is generated or prepended to the columns named
   219      * no correlation name is generated or prepended to the columns named
   220      * in the second parameter.
   220      * in the second parameter.
   221      *
   221      *
   222      * @param  array|string|Zend_Db_Expr $name The table name or an associative array relating table name to
   222      * @param  array|string|Zend_Db_Expr $name The table name or an associative array
   223      *                                         correlation name.
   223      *                                         relating correlation name to table name.
   224      * @param  array|string|Zend_Db_Expr $cols The columns to select from this table.
   224      * @param  array|string|Zend_Db_Expr $cols The columns to select from this table.
   225      * @param  string $schema The schema name to specify, if any.
   225      * @param  string $schema The schema name to specify, if any.
   226      * @return Zend_Db_Select This Zend_Db_Select object.
   226      * @return Zend_Db_Select This Zend_Db_Select object.
   227      */
   227      */
   228     public function from($name, $cols = '*', $schema = null)
   228     public function from($name, $cols = '*', $schema = null)
   878         }
   878         }
   879 
   879 
   880         $join  = $this->_adapter->quoteIdentifier(key($this->_parts[self::FROM]), true);
   880         $join  = $this->_adapter->quoteIdentifier(key($this->_parts[self::FROM]), true);
   881         $from  = $this->_adapter->quoteIdentifier($this->_uniqueCorrelation($name), true);
   881         $from  = $this->_adapter->quoteIdentifier($this->_uniqueCorrelation($name), true);
   882 
   882 
   883         $cond1 = $from . '.' . $cond;
   883         $joinCond = array();
   884         $cond2 = $join . '.' . $cond;
   884         foreach ((array)$cond as $fieldName) {
   885         $cond  = $cond1 . ' = ' . $cond2;
   885             $cond1 = $from . '.' . $fieldName;
       
   886             $cond2 = $join . '.' . $fieldName;
       
   887             $joinCond[]  = $cond1 . ' = ' . $cond2;
       
   888         }
       
   889         $cond = implode(' '.self::SQL_AND.' ', $joinCond);
   886 
   890 
   887         return $this->_join($type, $name, $cond, $cols, $schema);
   891         return $this->_join($type, $name, $cond, $cols, $schema);
   888     }
   892     }
   889 
   893 
   890     /**
   894     /**
   894      * @return string A unique correlation name.
   898      * @return string A unique correlation name.
   895      */
   899      */
   896     private function _uniqueCorrelation($name)
   900     private function _uniqueCorrelation($name)
   897     {
   901     {
   898         if (is_array($name)) {
   902         if (is_array($name)) {
   899             $c = end($name);
   903             $k = key($name);
       
   904             $c = is_string($k) ? $k : end($name);
   900         } else {
   905         } else {
   901             // Extract just the last name of a qualified table name
   906             // Extract just the last name of a qualified table name
   902             $dot = strrpos($name,'.');
   907             $dot = strrpos($name,'.');
   903             $c = ($dot === false) ? $name : substr($name, $dot+1);
   908             $c = ($dot === false) ? $name : substr($name, $dot+1);
   904         }
   909         }