Index: library/Zend/Db/Select.php =================================================================== --- library/Zend/Db/Select.php (revision 24490) +++ library/Zend/Db/Select.php (working copy) @@ -876,9 +876,31 @@ require_once 'Zend/Db/Select/Exception.php'; throw new Zend_Db_Select_Exception("You can only perform a joinUsing after specifying a FROM table"); } + + if (is_array($name)) { + // Must be array($correlationName => $tableName) or array($ident, ...) + foreach ($name as $_correlationName => $_tableName) { + if (is_string($_correlationName)) { + // We assume the key is the correlation name and value is the table name + $correlationName = $_correlationName; + } else { + // We assume just an array of identifiers, with no correlation name + $correlationName = $this->_uniqueCorrelation($name); + } + break; + } + } + else { + $correlationName = $this->_uniqueCorrelation($name); + } + if (empty($correlationName)) { + require_once 'Zend/Db/Select/Exception.php'; + throw new Zend_Db_Select_Exception("You specify a valid table when using joinUsing"); + } + $join = $this->_adapter->quoteIdentifier(key($this->_parts[self::FROM]), true); - $from = $this->_adapter->quoteIdentifier($this->_uniqueCorrelation($name), true); + $from = $this->_adapter->quoteIdentifier($correlationName, true); $cond1 = $from . '.' . $cond; $cond2 = $join . '.' . $cond; Index: tests/Zend/Db/Select/TestCommon.php =================================================================== --- tests/Zend/Db/Select/TestCommon.php (revision 24490) +++ tests/Zend/Db/Select/TestCommon.php (working copy) @@ -1698,4 +1698,24 @@ $this->assertType('string',$serialize); } + /** + * @group ZF-3309 + */ + public function testJoinUsingUsesTableNameOfTableBeingJoinedWhenAliasNotDefined() + { + $select = $this->_db->select(); + $select->from('table1')->joinUsing('table2', 'column1'); + $this->assertRegexp("/[`\"']?table2[`\"']?.[`\"']?column1[`\"']?/s", $select->assemble()); + } + + /** + * @group ZF-3309 + */ + public function testJoinUsingUsesAliasOfTableBeingJoinedWhenAliasIsDefined() + { + $select = $this->_db->select(); + $select->from('table1')->joinUsing(array('t2'=>'table2'), 'column1'); + $this->assertRegexp("/[`\"']?t2[`\"']?.[`\"']?column1[`\"']?/s", $select->assemble()); + } + }